• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

AndreuCodina / CrossValidation / 5596713550

19 Jul 2023 08:18AM UTC coverage: 94.783%. First build
5596713550

Pull #49

github

web-flow
Merge 7de26b7a5 into d3b2ca8c3
Pull Request #49: First version: business exception source generator

395 of 436 branches covered (90.6%)

Branch coverage included in aggregate %.

500 of 500 new or added lines in 35 files covered. (100.0%)

1331 of 1385 relevant lines covered (96.1%)

171.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.44
/src/CrossValidation/Exceptions/BusinessException.cs
1
using System.Net;
2
using System.Runtime.CompilerServices;
3
using System.Text.RegularExpressions;
4

5
namespace CrossValidation.Exceptions;
6

7
public class BusinessException : Exception
8
{
9
    private const string DefaultPlaceholderValue = "";
10
    private static readonly Regex PlaceholderRegex = new("{([^{}:]+)}", RegexOptions.Compiled);
2✔
11

12
    public override string Message => FormattedMessage;
305✔
13
    public string? Code { get; set;  } 
640✔
14
    public string FormattedMessage { get; set; } = "";
2,085✔
15
    public HttpStatusCode StatusCode { get; set; }
642✔
16
    public string? Details { get; set; }
659✔
17
    public string? FieldName { get; set; }
905✔
18
    public string? FieldDisplayName { get; set; }
828✔
19
    public Dictionary<string, object> PlaceholderValues { get; set; } = new();
2,238✔
20
    public Type? CrossErrorToException { get; set; }
234✔
21
    public Func<object>? GetFieldValue { get; set; }
610✔
22
    
23
    public BusinessException(
232✔
24
        string message = "",
232✔
25
        string? code = null,
232✔
26
        HttpStatusCode statusCode = HttpStatusCode.UnprocessableEntity,
232✔
27
        string? details = null,
232✔
28
        string? fieldName = null,
232✔
29
        string? fieldDisplayName = null)
232✔
30
    {
31
        FormattedMessage = GetFormattedMessage(code, message);
232✔
32
        Code = code;
232✔
33
        StatusCode = statusCode;
232✔
34
        Details = details;
232✔
35
        FieldName = fieldName;
232✔
36
        FieldDisplayName = fieldDisplayName;
232✔
37
        AddParametersAsPlaceholderValues();
232✔
38
        ReplacePlaceholderValues();
232✔
39
    }
232✔
40

41
    private static string GetFormattedMessage(string? code, string message)
42
    {
43
        if (code is not null && message == "")
232✔
44
        {
45
            return CrossValidationOptions.GetMessageFromCode(code) ?? message;
184!
46
        }
47

48
        return message;
48✔
49
    }
50
    
51
    protected void AddPlaceholderValue(
52
        object? value,
53
        [CallerArgumentExpression(nameof(value))] string name = default!)
54
    {
55
        PlaceholderValues ??= new();
632!
56

57
        if (PlaceholderValues.ContainsKey(name))
632!
58
        {
59
            throw new InvalidOperationException("Cannot add a placeholder with the same name twice");
×
60
        }
61

62
        PlaceholderValues.Add(name, value ?? DefaultPlaceholderValue);
632✔
63
    }
632✔
64

65
    public void AddCommonPlaceholderValues()
66
    {
67
        AddPlaceholderValue(FieldName, "fieldName");
187✔
68
        AddPlaceholderValue(FieldDisplayName, "fieldDisplayName");
187✔
69

70
        if (GetFieldValue is not null)
187✔
71
        {
72
            AddPlaceholderValue(GetFieldValue!(), "fieldValue");
187✔
73
        }
74
        
75
        ReplacePlaceholderValues();
187✔
76
    }
187✔
77

78
    public virtual void AddParametersAsPlaceholderValues()
79
    {
80
    }
166✔
81

82
    private void ReplacePlaceholderValues()
83
    {
84
        if (FormattedMessage == "")
419✔
85
        {
86
            return;
64✔
87
        }
88
        
89
        FormattedMessage = PlaceholderRegex.Replace(FormattedMessage, evaluator =>
355✔
90
        {
355✔
91
            var key = evaluator.Groups[1].Value;
93✔
92

355✔
93
            if (!PlaceholderValues.TryGetValue(key, out var value))
93✔
94
            {
355✔
95
                return evaluator.Value;
58✔
96
            }
355✔
97
            
355✔
98
            return value.ToString()!;
35✔
99
        });
355✔
100
    }
355✔
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc