77using Microsoft . AspNetCore . WebUtilities ;
88using Microsoft . Extensions . Options ;
99using PowerUtils . Net . Constants ;
10- using PowerUtils . Text ;
1110
1211namespace PowerUtils . AspNetCore . ErrorHandler
1312{
@@ -22,16 +21,14 @@ internal static ProblemDetailsResponse Create(HttpContext httpContext)
2221 {
2322 var result = new ProblemDetailsResponse ( ) ;
2423
25- result . Status = httpContext . GetStatusCode ( ) ?? 0 ;
24+ result . Status = httpContext . GetStatusCode ( ) ?? 0 ; // Default value is 0
2625 result . Type = result . Status . GetStatusCodeLinkOrDefault ( ) ;
2726 result . Title = result . Status == 0 ? null : ReasonPhrases . GetReasonPhrase ( result . Status ) ;
2827
2928 result . Instance = httpContext . GetRequestEndpoint ( ) ;
3029
3130 result . TraceID = httpContext . GetCorrelationId ( ) ;
3231
33- result . Errors = new Dictionary < string , string > ( ) ;
34-
3532 return result ;
3633 }
3734
@@ -74,12 +71,6 @@ private IDictionary<string, string> _mappingModelState(ModelStateDictionary mode
7471
7572 private ( string Property , string Error ) _mappingModelStateError ( KeyValuePair < string , ModelStateEntry > modelStateError )
7673 {
77- var error = modelStateError
78- . Value
79- . Errors
80- . Select ( s => s . ErrorMessage )
81- . First ( ) ;
82-
8374 if ( modelStateError . Key . StartsWith ( "$." ) )
8475 {
8576 return (
@@ -88,6 +79,12 @@ private IDictionary<string, string> _mappingModelState(ModelStateDictionary mode
8879 ) ;
8980 }
9081
82+ var error = modelStateError
83+ . Value
84+ . Errors
85+ . Select ( s => s . ErrorMessage )
86+ . First ( ) ;
87+
9188 return _checkRequestBody ( modelStateError . Key , error ) ;
9289 }
9390
@@ -111,39 +108,9 @@ private string _formatPropertyName(string propertyName)
111108 => _options . Value . PropertyNamingPolicy switch
112109 {
113110 PropertyNamingPolicy . Original => propertyName ,
114- PropertyNamingPolicy . SnakeCase => _formatPropertyToSnakeCase ( propertyName ) ,
115- _ => _formatPropertyToCamelCase ( propertyName ) ,
111+ PropertyNamingPolicy . SnakeCase => propertyName . FormatToSnakeCase ( ) ,
112+ _ => propertyName . FormatToCamelCase ( ) ,
116113 } ;
117114
118- private static string _formatPropertyToCamelCase ( string propertyName )
119- {
120- var propertyParts = propertyName . Split ( '.' ) ;
121- if ( propertyParts . Length == 1 )
122- {
123- return char . ToLowerInvariant ( propertyName [ 0 ] ) + propertyName [ 1 ..] ;
124- }
125-
126-
127- for ( var count = 0 ; count < propertyParts . Length ; count ++ )
128- {
129- propertyParts [ count ] = char . ToLowerInvariant ( propertyParts [ count ] [ 0 ] ) + propertyParts [ count ] [ 1 ..] ;
130- }
131-
132-
133- return string . Join ( "." , propertyParts ) ;
134- }
135-
136- private static string _formatPropertyToSnakeCase ( string propertyName )
137- {
138- var propertyParts = propertyName . Split ( '.' ) ;
139-
140- for ( var count = 0 ; count < propertyParts . Length ; count ++ )
141- {
142- propertyParts [ count ] = propertyParts [ count ] . ToSnakeCase ( ) ;
143- }
144-
145-
146- return string . Join ( "." , propertyParts ) ;
147- }
148115 }
149116}
0 commit comments