diff --git a/doppler-beplic/Models/Responses/ErrorResponse.cs b/doppler-beplic/Models/Responses/ErrorResponse.cs new file mode 100644 index 0000000..6648c7f --- /dev/null +++ b/doppler-beplic/Models/Responses/ErrorResponse.cs @@ -0,0 +1,10 @@ + +namespace DopplerBeplic.Models.Responses +{ + public class ErrorResponse + { + public string? Status { get; set; } + public string? Message { get; set; } + public List? Errors { get; set; } + } +} diff --git a/doppler-beplic/Services/Classes/BeplicService.cs b/doppler-beplic/Services/Classes/BeplicService.cs index b9f11f4..8483a1a 100644 --- a/doppler-beplic/Services/Classes/BeplicService.cs +++ b/doppler-beplic/Services/Classes/BeplicService.cs @@ -20,8 +20,8 @@ public BeplicService(IOptions options, BeplicSdk sdk, ILogg _logger = logger; } - [LoggerMessage(0, LogLevel.Debug, "Unsuccesfull response from Beplic API for UserId:{UserId}. Response: {Response} Status: {Status}")] - partial void LogDebugBadRequest(string userId, string response, string status); + [LoggerMessage(0, LogLevel.Information, "Unsuccesfull response from Beplic API for UserId:{UserId}. Response: {Response} Status: {Status}")] + partial void LogInfoBadRequest(string userId, string response, string status); [LoggerMessage(1, LogLevel.Error, "Unexpected exception thrown for UserId:{UserId}.")] partial void LogErrorException(string userId, Exception e); @@ -66,30 +66,12 @@ public async Task CreateUser(UserCreationDTO accountData) } else { - LogDebugBadRequest(accountData.Customer.IdExternal, response.Content ?? "", response.StatusCode.ToString()); - var deserealizedResponse = JsonConvert.DeserializeAnonymousType(response.Content ?? "", - new - { - errors = new[] - { - new { - status = string.Empty, - title = string.Empty, - detail = string.Empty, - source = new - { - pointer = string.Empty - } - } - }.ToList() - }); - - //TODO: Verify with beplic if the array of errors it's realy needed. - var error = deserealizedResponse?.errors.FirstOrDefault(); + LogInfoBadRequest(accountData.Customer.IdExternal, response.Content ?? "", response.StatusCode.ToString()); + var deserealizedResponse = JsonConvert.DeserializeAnonymousType(response.Content ?? "", new ErrorResponse()); result.Success = false; - result.ErrorStatus = error?.status; - result.Error = error?.detail; + result.ErrorStatus = deserealizedResponse?.Status; + result.Error = deserealizedResponse?.Message; } } catch (Exception ex) @@ -134,30 +116,12 @@ public async Task UpdateCustomer(CustomerUpdateDTO custo } else { - LogDebugBadRequest(customerData.Customer.IdExternal, response.Content ?? "", response.StatusCode.ToString()); - var deserealizedResponse = JsonConvert.DeserializeAnonymousType(response.Content ?? string.Empty, - new - { - errors = new[] - { - new { - status = string.Empty, - title = string.Empty, - detail = string.Empty, - source = new - { - pointer = string.Empty - } - } - }.ToList() - }); - - //TODO: Verify with beplic if the array of errors it's realy needed. - var error = deserealizedResponse?.errors.FirstOrDefault(); + LogInfoBadRequest(customerData.Customer.IdExternal, response.Content ?? "", response.StatusCode.ToString()); + var deserealizedResponse = JsonConvert.DeserializeAnonymousType(response.Content ?? string.Empty, new ErrorResponse()); result.Success = false; - result.ErrorStatus = error?.status; - result.Error = error?.detail; + result.ErrorStatus = deserealizedResponse?.Status; + result.Error = deserealizedResponse?.Message; } } catch (Exception ex) @@ -210,30 +174,12 @@ public async Task UpdateUserAdmin(UserAdminUpdateDTO us } else { - LogDebugBadRequest(userAdminData.IdExternal, response.Content ?? "", response.StatusCode.ToString()); - var deserealizedResponse = JsonConvert.DeserializeAnonymousType(response.Content ?? string.Empty, - new - { - errors = new[] - { - new { - status = string.Empty, - title = string.Empty, - detail = string.Empty, - source = new - { - pointer = string.Empty - } - } - }.ToList() - }); - - //TODO: Verify with beplic if the array of errors it's realy needed. - var error = deserealizedResponse?.errors.FirstOrDefault(); + LogInfoBadRequest(userAdminData.IdExternal, response.Content ?? "", response.StatusCode.ToString()); + var deserealizedResponse = JsonConvert.DeserializeAnonymousType(response.Content ?? string.Empty, new ErrorResponse()); result.Success = false; - result.ErrorStatus = error?.status; - result.Error = error?.detail; + result.ErrorStatus = deserealizedResponse?.Status; + result.Error = deserealizedResponse?.Message; } } catch (Exception ex)