88
99namespace PowerUtils . AspNetCore . ErrorHandler
1010{
11- public sealed class ApiProblemDetailsFactory : ProblemDetailsFactory
11+ internal sealed class ApiProblemDetailsFactory : ProblemDetailsFactory , IProblemFactory
1212 {
13+ private readonly IHttpContextAccessor _httpContextAccessor ;
14+
1315 private readonly ApiBehaviorOptions _apiBehaviorOptions ;
1416 private readonly ErrorHandlerOptions _errorHandlerOptions ;
1517
1618 public ApiProblemDetailsFactory (
19+ IHttpContextAccessor httpContextAccessor ,
1720 IOptions < ApiBehaviorOptions > apiBehaviorOptions ,
1821 IOptions < ErrorHandlerOptions > errorHandlerOptions
1922 )
2023 {
24+ _httpContextAccessor = httpContextAccessor ;
25+
2126 _apiBehaviorOptions = apiBehaviorOptions ? . Value ?? throw new ArgumentNullException ( nameof ( apiBehaviorOptions ) ) ;
2227 _errorHandlerOptions = errorHandlerOptions ? . Value ?? throw new ArgumentNullException ( nameof ( errorHandlerOptions ) ) ;
2328 }
2429
30+
31+ public ObjectResult CreateProblemResult (
32+ string detail = null ,
33+ string instance = null ,
34+ int ? statusCode = null ,
35+ string title = null ,
36+ string type = null ,
37+ IDictionary < string , string > errors = null
38+ ) => new ObjectResult ( CreateProblem (
39+ detail ,
40+ instance ,
41+ statusCode ,
42+ title ,
43+ type ,
44+ errors
45+ ) ) ;
46+
47+ public ErrorProblemDetails CreateProblem (
48+ string detail = null ,
49+ string instance = null ,
50+ int ? statusCode = null ,
51+ string title = null ,
52+ string type = null ,
53+ IDictionary < string , string > errors = null
54+ )
55+ {
56+ var problemDetails = new ErrorProblemDetails
57+ {
58+ Status = statusCode ,
59+ Title = title ,
60+ Type = type ,
61+ Detail = detail ,
62+ Instance = instance ,
63+ Errors = errors
64+ } ;
65+
66+ _applyDefaults ( _httpContextAccessor . HttpContext , problemDetails ) ;
67+
68+ return problemDetails ;
69+ }
70+
2571 internal ErrorProblemDetails Create ( HttpContext httpContext )
2672 {
2773 var problemDetails = new ErrorProblemDetails ( ) ;
@@ -31,7 +77,6 @@ internal ErrorProblemDetails Create(HttpContext httpContext)
3177 return problemDetails ;
3278 }
3379
34-
3580 public ErrorProblemDetails Create ( HttpContext httpContext , IEnumerable < KeyValuePair < string , string > > errors )
3681 {
3782 var result = Create ( httpContext ) ;
@@ -146,14 +191,14 @@ private void _applyDefaults(HttpContext httpContext, ProblemDetails problemDetai
146191 problemDetails . Title ??= ProblemDetailsDefaults . Defaults [ 0 ] . Title ;
147192 }
148193
149- problemDetails . Instance = httpContext . GetRequestEndpoint ( ) ;
194+ problemDetails . Instance ?? = httpContext . GetRequestEndpoint ( ) ;
150195
151196 problemDetails . Extensions [ "traceId" ] = httpContext . GetCorrelationId ( ) ;
152197
153- _applyDetails ( problemDetails ) ;
198+ _applyDetail ( problemDetails ) ;
154199 }
155200
156- private static void _applyDetails ( ProblemDetails problemDetails )
201+ private static void _applyDetail ( ProblemDetails problemDetails )
157202 {
158203 if ( ! string . IsNullOrWhiteSpace ( problemDetails . Detail ) )
159204 {
0 commit comments