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

AndreuCodina / CrossValidation / 5940305339

22 Aug 2023 02:36PM UTC coverage: 92.715% (-2.7%) from 95.391%
5940305339

push

github

web-flow
Problem Details service MVP (#74)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: dependabot[bot] <dependabot[bot]@users.noreply.github.com>
Co-authored-by: AndreuCodina <AndreuCodina@users.noreply.github.com>

431 of 486 branches covered (88.68%)

Branch coverage included in aggregate %.

299 of 299 new or added lines in 21 files covered. (100.0%)

1389 of 1477 relevant lines covered (94.04%)

176.66 hits per line

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

95.71
/src/CrossValidation/DependencyInjection/ProblemDetailsMapper.cs
1
using System.Net;
2
using CrossValidation.Exceptions;
3
using Microsoft.AspNetCore.Mvc;
4

5
namespace CrossValidation.DependencyInjection;
6

7
internal class ProblemDetailsMapper(ProblemDetails problemDetails)
36✔
8
{
9
    public void Map(BusinessException businessException)
10
    {
11
        SetStatusDetails(problemDetails, businessException.StatusCode);
33✔
12
        var errors = new List<CrossValidationProblemDetailsError>();
33✔
13
        var error = CreateCrossProblemDetailsError(businessException);
33✔
14
        var allErrorCustomizationsAreNotSet =
33✔
15
            error is
33✔
16
            {
33✔
17
                Code: null,
33✔
18
                Message: null,
33✔
19
                Details: null,
33✔
20
                Placeholders: null
33✔
21
            };
33✔
22

23
        if (!allErrorCustomizationsAreNotSet)
33✔
24
        {
25
            errors.Add(error);
19✔
26
        }
27

28
        AddExtensions(problemDetails, errors);
33✔
29
    }
33✔
30
    
31
    public void Map(BusinessListException businessListException)
32
    {
33
        SetStatusDetails(problemDetails, (int)HttpStatusCode.UnprocessableEntity);
3✔
34
        var errors = new List<CrossValidationProblemDetailsError>();
3✔
35

36
        foreach (var error in businessListException.Exceptions)
6!
37
        {
38
            errors.Add(CreateCrossProblemDetailsError(error));
×
39
        }
40

41
        AddExtensions(problemDetails, errors);
3✔
42
    }
3✔
43

44
    private static void AddExtensions(ProblemDetails problemDetails, List<CrossValidationProblemDetailsError> errors)
45
    {
46
        if (errors.Count > 0)
36✔
47
        {
48
            problemDetails.Extensions.Add("errors", errors);
19✔
49
        }
50
    }
36✔
51
    
52
    private CrossValidationProblemDetailsError CreateCrossProblemDetailsError(BusinessException exception)
53
    {
54
        var error = new CrossValidationProblemDetailsError
33✔
55
        {
33✔
56
            Code = exception.Code,
33✔
57
            CodeUrl = null,
33✔
58
            Message = exception.Message == "" ? null : exception.Message,
33✔
59
            Details = exception.Details,
33✔
60
            FieldName = exception.FieldName,
33✔
61
            Placeholders = GetPlaceholders(exception)
33✔
62
        };
33✔
63

64
        return error;
33✔
65
    }
66

67
    private Dictionary<string, object?>? GetPlaceholders(BusinessException exception)
68
    {
69
        var hasToSendPlaceholders =
33!
70
            CrossValidationOptions.LocalizeCommonErrorsInFront
33✔
71
            && exception.PlaceholderValues.Count > 0;
33✔
72

73
        if (exception is not FrontBusinessException && !hasToSendPlaceholders)
33✔
74
        {
75
            return null;
32✔
76
        }
77
        
78
        var placeholders = new Dictionary<string, object?>();
1✔
79

80
        foreach (var placeholder in exception.PlaceholderValues)
10✔
81
        {
82
            placeholders.Add(placeholder.Key, placeholder.Value);
4✔
83
        }
84

85
        return placeholders;
1✔
86
    }
87

88
    private void SetStatusDetails(ProblemDetails problemDetails, int statusCode)
89
    {
90
        CrossValidationProblemDetailsDefaults.Apply(problemDetails, statusCode);
36✔
91
    }
36✔
92
    
93
    // private string? GetPublicationUrl(BusinessException exception, HttpContext context)
94
    // {
95
    //     string? baseUrl = null;
96
    //     
97
    //     if (!CrossValidationOptions.IsErrorCodePageEnabled || exception.Code is null)
98
    //     {
99
    //         return null;
100
    //     }
101
    //
102
    //     if (CrossValidationOptions.PublicationUrl is not null)
103
    //     {
104
    //         baseUrl = CrossValidationOptions.PublicationUrl;
105
    //     }
106
    //     else if (_environment.IsDevelopment())
107
    //     {
108
    //         var protocol = context.Request.IsHttps ? "https" : "http";
109
    //         var host = context.Request.Host.Value;
110
    //         var port = context.Request.Host.Port is not null
111
    //             ? $":{context.Request.Host.Port.Value}"
112
    //             : null;
113
    //         baseUrl = $"{protocol}://{host}{port}";
114
    //     }
115
    //     else
116
    //     {
117
    //         return null;
118
    //     }
119
    //     
120
    //     return $"{baseUrl}{CrossValidationOptions.ErrorCodePagePath}#{exception.Code}";
121
    // }
122
}
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

© 2025 Coveralls, Inc