Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOI-1111] Fix error deserealization #43

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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