@@ -6,16 +6,41 @@ namespace Digdir.Domain.Dialogporten.WebApi.Common.Extensions;
6
6
7
7
internal static class ErrorResponseBuilderExtensions
8
8
{
9
- public static object ResponseBuilder ( this HttpContext ctx , int statusCode , List < ValidationFailure > ? failures = null ) =>
10
- ResponseBuilder ( failures ?? [ ] , ctx , statusCode ) ;
9
+ public static ProblemDetails DefaultResponse ( this HttpContext ctx , int ? statusCode = null ) => new ( )
10
+ {
11
+ Title = "An error occurred while processing the request." ,
12
+ Detail = "Something went wrong during the request." ,
13
+ Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.6.1" ,
14
+ Status = statusCode ?? ctx . Response . StatusCode ,
15
+ Instance = ctx . Request . Path ,
16
+ Extensions = { { "traceId" , Activity . Current ? . Id ?? ctx . TraceIdentifier } }
17
+ } ;
18
+
19
+ public static ProblemDetails GetResponseOrDefault ( this HttpContext ctx , int statusCode ,
20
+ List < ValidationFailure > ? failures = null ) =>
21
+ ctx . ResponseBuilder ( failures , statusCode ) ?? ctx . DefaultResponse ( statusCode ) ;
11
22
12
23
public static object ResponseBuilder ( List < ValidationFailure > failures , HttpContext ctx , int statusCode )
24
+ => ctx . ResponseBuilder ( failures , statusCode ) ?? ctx . DefaultResponse ( statusCode ) ;
25
+
26
+ public static ProblemDetails ? ResponseBuilder ( this HttpContext ctx , List < ValidationFailure > ? failures = null , int ? statusCode = null )
13
27
{
14
- var errors = failures
28
+ var errors = failures ?
15
29
. GroupBy ( f => f . PropertyName )
16
- . ToDictionary ( x => x . Key , x => x . Select ( m => m . ErrorMessage ) . ToArray ( ) ) ;
30
+ . ToDictionary ( x => x . Key , x => x . Select ( m => m . ErrorMessage ) . ToArray ( ) )
31
+ ?? [ ] ;
32
+
33
+ statusCode ??= ctx . Response . StatusCode ;
17
34
return statusCode switch
18
35
{
36
+ StatusCodes . Status413PayloadTooLarge => new ProblemDetails
37
+ {
38
+ Title = $ "Payload too large. The maximum allowed size is { Constants . MaxRequestBodySize } bytes.",
39
+ Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.11" ,
40
+ Status = statusCode ,
41
+ Instance = ctx . Request . Path ,
42
+ Extensions = { { "traceId" , Activity . Current ? . Id ?? ctx . TraceIdentifier } }
43
+ } ,
19
44
StatusCodes . Status400BadRequest => new ValidationProblemDetails ( errors )
20
45
{
21
46
Title = "One or more validation errors occurred." ,
@@ -73,15 +98,7 @@ public static object ResponseBuilder(List<ValidationFailure> failures, HttpConte
73
98
Instance = ctx . Request . Path ,
74
99
Extensions = { { "traceId" , Activity . Current ? . Id ?? ctx . TraceIdentifier } }
75
100
} ,
76
- _ => new ProblemDetails
77
- {
78
- Title = "An error occurred while processing the request." ,
79
- Detail = "Something went wrong during the request." ,
80
- Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.6.1" ,
81
- Status = ctx . Response . StatusCode ,
82
- Instance = ctx . Request . Path ,
83
- Extensions = { { "traceId" , Activity . Current ? . Id ?? ctx . TraceIdentifier } }
84
- }
101
+ _ => null
85
102
} ;
86
103
}
87
104
}
0 commit comments