7
7
using StrDss . Data . Repositories ;
8
8
using StrDss . Data . Entities ;
9
9
using NetTopologySuite . Geometries ;
10
+ using System . Text . RegularExpressions ;
11
+ using System . Text . Json ;
10
12
11
13
namespace StrDss . Service
12
14
{
15
+ public class ApiErrorResponse
16
+ {
17
+ public string ErrorMessage { get ; set ; }
18
+ public string RootCause { get ; set ; }
19
+ }
20
+
13
21
public interface IPermitValidationService
14
22
{
15
23
Task < ( bool isValid , string registrationText ) > ValidateRegistrationPermitAsync ( string regNo , string unitNumber , string streetNumber , string postalCode ) ;
@@ -51,10 +59,11 @@ public PermitValidationService(IRegistrationApiClient regApiClient, IGeocoderApi
51
59
}
52
60
} ;
53
61
62
+ Response resp ;
54
63
try
55
64
{
56
65
_logger . LogInformation ( "Calling validate permit." ) ;
57
- Response resp = await _regApiClient . ValidatePermitAsync ( body , _apiAccount ) ;
66
+ resp = await _regApiClient . ValidatePermitAsync ( body , _apiAccount ) ;
58
67
59
68
// If we didn't get a Status field back, then there was an error
60
69
if ( string . IsNullOrEmpty ( resp . Status ) )
@@ -67,27 +76,36 @@ public PermitValidationService(IRegistrationApiClient regApiClient, IGeocoderApi
67
76
else
68
77
{
69
78
_logger . LogInformation ( "Validate permit returned an error." ) ;
70
- Dictionary < string , List < string > > errorDetails = resp . Errors
71
- . GroupBy ( e => e . Code )
72
- . ToDictionary ( g => g . Key , g => g . Select ( e => e . Message ) . ToList ( ) ) ;
73
- registrationText = errorDetails . ParseError ( ) ;
79
+ List < string > errorDetails = resp . Errors
80
+ . Select ( e => $ "{ e . Code } : { e . Message } ")
81
+ . ToList ( ) ;
82
+
83
+ registrationText = string . Join ( "\n " , resp . Errors . Select ( e => $ "{ e . Code } : { e . Message } ") ) ;
74
84
}
75
85
}
76
- else if ( ! string . Equals ( resp . Status , "ACTIVE" , StringComparison . OrdinalIgnoreCase ) )
86
+ else
77
87
{
78
- _logger . LogInformation ( "Permit status is not ACTIVE." ) ;
79
- isValid = false ;
80
- registrationText = resp . Status ;
88
+ _logger . LogInformation ( "Permit status is: ." + resp . Status ) ;
81
89
90
+ registrationText = resp . Status ;
82
91
}
83
92
}
84
- catch ( ApiException ex )
93
+ catch ( ApiException ex ) when ( ex . StatusCode == 401 )
85
94
{
86
- _logger . LogInformation ( "Validate permit call threw an Api exception : " + ex . Message ) ;
95
+ _logger . LogInformation ( "Validate permit call return 401 : " + ex . Message ) ;
87
96
isValid = false ;
88
- registrationText = ex . StatusCode == 404 ? RegistrationValidationText . ValidationException404 :
89
- ex . StatusCode == 401 ? RegistrationValidationText . ValidationException401 :
90
- RegistrationValidationText . ValidationException ;
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 ;
104
+
105
+ }
106
+ catch ( ApiException ex )
107
+ {
108
+ registrationText = HandleApiException ( ex ) ;
91
109
}
92
110
catch ( Exception ex )
93
111
{
@@ -156,4 +174,38 @@ public PermitValidationService(IRegistrationApiClient regApiClient, IGeocoderApi
156
174
157
175
return ( isExempt , registrationText ) ;
158
176
}
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
+ var errorResponse = JsonSerializer . Deserialize < ApiErrorResponse > ( ex . Response ) ;
194
+ if ( errorResponse ? . RootCause != null )
195
+ {
196
+ var match = Regex . Match ( errorResponse . RootCause , @"code:(?<code>[^,]+),message:(?<message>[^,\]]+)" ) ;
197
+ if ( match . Success )
198
+ {
199
+ string code = match . Groups [ "code" ] . Value ;
200
+ string message = match . Groups [ "message" ] . Value ;
201
+ return $ "{ code } : { message } ";
202
+ }
203
+ else
204
+ {
205
+ return errorResponse . RootCause ; // Fallback to the raw rootCause
206
+ }
207
+ }
208
+ }
209
+ return RegistrationValidationText . ValidationException ;
210
+ }
159
211
}
0 commit comments