@@ -77,39 +77,64 @@ public PermitValidationService(IRegistrationApiClient regApiClient, IGeocoderApi
77
77
{
78
78
_logger . LogInformation ( "Validate permit returned an error." ) ;
79
79
List < string > errorDetails = resp . Errors
80
- . Select ( e => $ "{ e . Code } : { e . Message } ")
80
+ . Select ( e => $ "{ e . Code } :{ e . Message } ")
81
81
. ToList ( ) ;
82
82
83
- registrationText = string . Join ( "\n " , resp . Errors . Select ( e => $ "{ e . Code } : { e . Message } ") ) ;
83
+ registrationText = string . Join ( "\n " , resp . Errors . Select ( e => $ "{ e . Code } :{ e . Message } ") ) ;
84
84
}
85
85
}
86
86
else
87
87
{
88
88
_logger . LogInformation ( "Permit status is: ." + resp . Status ) ;
89
89
90
- registrationText = resp . Status ;
90
+ registrationText = "200:" + resp . Status ;
91
91
}
92
92
}
93
- catch ( ApiException ex ) when ( ex . StatusCode == 401 )
93
+ catch ( ApiException < Response2 > ex )
94
94
{
95
- _logger . LogInformation ( "Validate permit call return 401: " + ex . Message ) ;
96
95
isValid = false ;
97
- registrationText = RegistrationValidationText . ValidationException401 ;
98
- }
99
- catch ( ApiException ex ) when ( ex . StatusCode == 404 )
100
- {
101
- _logger . LogInformation ( "Validate permit call returned 404: " + ex . Message ) ;
102
- isValid = false ;
103
- registrationText = RegistrationValidationText . ValidationException404 ;
96
+ _logger . LogError ( $ "Error Code: { ex . StatusCode } , Additional Properties: { string . Join ( ", " , ex . Result . AdditionalProperties ) } ") ;
97
+ // Extract the rootCause from AdditionalProperties
98
+ if ( ex . Result . AdditionalProperties . TryGetValue ( "rootCause" , out var rootCauseObj ) && rootCauseObj is string rootCause )
99
+ {
100
+ // Use regex to extract the specific error message
101
+ var match = Regex . Match ( rootCause , @"message:(?<message>[^\]]+)" ) ;
102
+ if ( match . Success )
103
+ {
104
+ string errorMessage = match . Groups [ "message" ] . Value . Trim ( ) ;
105
+ registrationText = $ "{ ex . StatusCode } :{ errorMessage } ";
106
+ }
107
+ else
108
+ {
109
+ registrationText = $ "{ ex . StatusCode } :Unknown error in rootCause.";
110
+ }
111
+ }
112
+ else
113
+ {
114
+ registrationText = $ "{ ex . StatusCode } :Unknown error.";
115
+ }
104
116
105
117
}
106
118
catch ( ApiException ex )
107
119
{
108
- registrationText = HandleApiException ( ex ) ;
120
+ isValid = false ;
121
+ if ( ex . StatusCode == 401 )
122
+ {
123
+ registrationText = ex . StatusCode + ":" + RegistrationValidationText . ValidationException401 ;
124
+ }
125
+ else if ( ex . StatusCode == 404 )
126
+ {
127
+ registrationText = ex . StatusCode + ":" + RegistrationValidationText . ValidationException404 ;
128
+ }
129
+ else
130
+ {
131
+ registrationText = ex . StatusCode + ":" + RegistrationValidationText . ValidationException ;
132
+ }
133
+ _logger . LogError ( $ "Validate permit call return { ex . StatusCode } : { ex . Message } ") ;
109
134
}
110
135
catch ( Exception ex )
111
136
{
112
- _logger . LogInformation ( "Validate permit call threw an exception: " + ex . Message ) ;
137
+ _logger . LogError ( "Validate permit call threw an exception: " + ex . Message ) ;
113
138
isValid = false ;
114
139
registrationText = RegistrationValidationText . ValidationException ;
115
140
}
@@ -174,40 +199,4 @@ public PermitValidationService(IRegistrationApiClient regApiClient, IGeocoderApi
174
199
175
200
return ( isExempt , registrationText ) ;
176
201
}
177
-
178
- private string HandleApiException ( ApiException ex )
179
- {
180
- if ( ex . StatusCode == 401 )
181
- {
182
- _logger . LogInformation ( "Validate permit call return 401: " + ex . Message ) ;
183
- return RegistrationValidationText . ValidationException401 ;
184
- }
185
- if ( ex . StatusCode == 404 )
186
- {
187
- _logger . LogInformation ( "Validate permit call returned 404: " + ex . Message ) ;
188
- return RegistrationValidationText . ValidationException404 ;
189
-
190
- }
191
- if ( ex . StatusCode == 400 )
192
- {
193
- _logger . LogInformation ( "Validate permit call returned 400." ) ;
194
- return RegistrationValidationText . ValidationException ;
195
- //var errorResponse = JsonSerializer.Deserialize<ApiErrorResponse>(ex.Response);
196
- //if (errorResponse?.RootCause != null)
197
- //{
198
- // var match = Regex.Match(errorResponse.RootCause, @"code:(?<code>[^,]+),message:(?<message>[^,\]]+)");
199
- // if (match.Success)
200
- // {
201
- // string code = match.Groups["code"].Value;
202
- // string message = match.Groups["message"].Value;
203
- // return $"{code}: {message}";
204
- // }
205
- // else
206
- // {
207
- // return errorResponse.RootCause; // Fallback to the raw rootCause
208
- // }
209
- //}
210
- }
211
- return RegistrationValidationText . ValidationException ;
212
- }
213
202
}
0 commit comments