Skip to content

feat: log json deserialize error for service agent #179

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

Merged
merged 1 commit into from
Nov 20, 2023
Merged
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
45 changes: 31 additions & 14 deletions src/Cnblogs.Architecture.Ddd.Cqrs.ServiceAgent/CqrsServiceAgent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using Cnblogs.Architecture.Ddd.Cqrs.Abstractions;
using Cnblogs.Architecture.Ddd.Domain.Abstractions;
using Cnblogs.Architecture.Ddd.Infrastructure.Abstractions;
Expand Down Expand Up @@ -263,20 +264,28 @@ private static async Task<CommandResponse<TResponse, TError>> HandleCommandRespo
return CommandResponse<TResponse, TError>.Success();
}

if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
try
{
var result = await httpResponseMessage.Content.ReadFromJsonAsync<TResponse>();
return CommandResponse<TResponse, TError>.Success(result);
}
if (httpResponseMessage.StatusCode == HttpStatusCode.OK)
{
var result = await httpResponseMessage.Content.ReadFromJsonAsync<TResponse>();
return CommandResponse<TResponse, TError>.Success(result);
}

var response = await httpResponseMessage.Content.ReadFromJsonAsync<CommandResponse<TResponse, TError>>();
if (response is null)
{
throw new InvalidOperationException(
$"Could not deserialize error from response, response: {await httpResponseMessage.Content.ReadAsStringAsync()}");
}

var response = await httpResponseMessage.Content.ReadFromJsonAsync<CommandResponse<TResponse, TError>>();
if (response is null)
return response;
}
catch (JsonException)
{
throw new InvalidOperationException(
$"Could not deserialize error from response, response: {await httpResponseMessage.Content.ReadAsStringAsync()}");
$"Deserialize response failed, status code: {httpResponseMessage.StatusCode}, Body:{await httpResponseMessage.Content.ReadAsStringAsync()}");
}

return response;
}

private static async Task<CommandResponse<TError>> HandleCommandResponseAsync(HttpResponseMessage message)
Expand All @@ -286,13 +295,21 @@ private static async Task<CommandResponse<TError>> HandleCommandResponseAsync(Ht
return CommandResponse<TError>.Success();
}

var response = await message.Content.ReadFromJsonAsync<CommandResponse<TError>>();
if (response is null)
try
{
var response = await message.Content.ReadFromJsonAsync<CommandResponse<TError>>();
if (response is null)
{
throw new InvalidOperationException(
$"Could not deserialize error from response, response: {await message.Content.ReadAsStringAsync()}");
}

return response;
}
catch (JsonException)
{
throw new InvalidOperationException(
$"Could not deserialize error from response, response: {await message.Content.ReadAsStringAsync()}");
$"Deserialize response failed, status code: {message.StatusCode}, Body:{await message.Content.ReadAsStringAsync()}");
}

return response;
}
}