Skip to content

Commit

Permalink
Merge pull request #43 from FromDoppler/doi-1111-fix-error-serialization
Browse files Browse the repository at this point in the history
[DOI-1111] Fix error deserealization
  • Loading branch information
fgchaio authored Oct 12, 2023
2 parents f2663dd + 8b444b5 commit f40547e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 68 deletions.
10 changes: 10 additions & 0 deletions doppler-beplic/Models/Responses/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

namespace DopplerBeplic.Models.Responses
{
public class ErrorResponse
{
public string? Status { get; set; }
public string? Message { get; set; }
public List<string>? Errors { get; set; }
}
}
82 changes: 14 additions & 68 deletions doppler-beplic/Services/Classes/BeplicService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public BeplicService(IOptions<DefaulValuesOptions> 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);
Expand Down Expand Up @@ -66,30 +66,12 @@ public async Task<UserCreationResponse> 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)
Expand Down Expand Up @@ -134,30 +116,12 @@ public async Task<CustomerUpdateResponse> 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)
Expand Down Expand Up @@ -210,30 +174,12 @@ public async Task<UserAdminUpdateResponse> 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)
Expand Down

0 comments on commit f40547e

Please sign in to comment.