Skip to content

Commit 1786875

Browse files
authored
Merge pull request #895 from bcgov/feature/registration-validation
DSS-1139 Validation Report - response code update
2 parents c696857 + 69daf53 commit 1786875

File tree

1 file changed

+39
-50
lines changed

1 file changed

+39
-50
lines changed

server/StrDss.Service/PermitValidationService.cs

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,64 @@ public PermitValidationService(IRegistrationApiClient regApiClient, IGeocoderApi
7777
{
7878
_logger.LogInformation("Validate permit returned an error.");
7979
List<string> errorDetails = resp.Errors
80-
.Select(e => $"{e.Code}: {e.Message}")
80+
.Select(e => $"{e.Code}:{e.Message}")
8181
.ToList();
8282

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}"));
8484
}
8585
}
8686
else
8787
{
8888
_logger.LogInformation("Permit status is: ." + resp.Status);
8989

90-
registrationText = resp.Status;
90+
registrationText = "200:"+resp.Status;
9191
}
9292
}
93-
catch (ApiException ex) when (ex.StatusCode == 401)
93+
catch (ApiException<Response2> ex)
9494
{
95-
_logger.LogInformation("Validate permit call return 401: " + ex.Message);
9695
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+
}
104116

105117
}
106118
catch (ApiException ex)
107119
{
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}");
109134
}
110135
catch (Exception ex)
111136
{
112-
_logger.LogInformation("Validate permit call threw an exception: " + ex.Message);
137+
_logger.LogError("Validate permit call threw an exception: " + ex.Message);
113138
isValid = false;
114139
registrationText = RegistrationValidationText.ValidationException;
115140
}
@@ -174,40 +199,4 @@ public PermitValidationService(IRegistrationApiClient regApiClient, IGeocoderApi
174199

175200
return (isExempt, registrationText);
176201
}
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-
}
213202
}

0 commit comments

Comments
 (0)