11using System . Net ;
22using System . Net . Http . Json ;
3+ using System . Text . Json ;
34using Cnblogs . Architecture . Ddd . Cqrs . Abstractions ;
45using Cnblogs . Architecture . Ddd . Domain . Abstractions ;
56using Cnblogs . Architecture . Ddd . Infrastructure . Abstractions ;
@@ -263,20 +264,28 @@ private static async Task<CommandResponse<TResponse, TError>> HandleCommandRespo
263264 return CommandResponse < TResponse , TError > . Success ( ) ;
264265 }
265266
266- if ( httpResponseMessage . StatusCode == HttpStatusCode . OK )
267+ try
267268 {
268- var result = await httpResponseMessage . Content . ReadFromJsonAsync < TResponse > ( ) ;
269- return CommandResponse < TResponse , TError > . Success ( result ) ;
270- }
269+ if ( httpResponseMessage . StatusCode == HttpStatusCode . OK )
270+ {
271+ var result = await httpResponseMessage . Content . ReadFromJsonAsync < TResponse > ( ) ;
272+ return CommandResponse < TResponse , TError > . Success ( result ) ;
273+ }
274+
275+ var response = await httpResponseMessage . Content . ReadFromJsonAsync < CommandResponse < TResponse , TError > > ( ) ;
276+ if ( response is null )
277+ {
278+ throw new InvalidOperationException (
279+ $ "Could not deserialize error from response, response: { await httpResponseMessage . Content . ReadAsStringAsync ( ) } ") ;
280+ }
271281
272- var response = await httpResponseMessage . Content . ReadFromJsonAsync < CommandResponse < TResponse , TError > > ( ) ;
273- if ( response is null )
282+ return response ;
283+ }
284+ catch ( JsonException )
274285 {
275286 throw new InvalidOperationException (
276- $ "Could not deserialize error from response, response: { await httpResponseMessage . Content . ReadAsStringAsync ( ) } ") ;
287+ $ "Deserialize response failed, status code: { httpResponseMessage . StatusCode } , Body: { await httpResponseMessage . Content . ReadAsStringAsync ( ) } ") ;
277288 }
278-
279- return response ;
280289 }
281290
282291 private static async Task < CommandResponse < TError > > HandleCommandResponseAsync ( HttpResponseMessage message )
@@ -286,13 +295,21 @@ private static async Task<CommandResponse<TError>> HandleCommandResponseAsync(Ht
286295 return CommandResponse < TError > . Success ( ) ;
287296 }
288297
289- var response = await message . Content . ReadFromJsonAsync < CommandResponse < TError > > ( ) ;
290- if ( response is null )
298+ try
299+ {
300+ var response = await message . Content . ReadFromJsonAsync < CommandResponse < TError > > ( ) ;
301+ if ( response is null )
302+ {
303+ throw new InvalidOperationException (
304+ $ "Could not deserialize error from response, response: { await message . Content . ReadAsStringAsync ( ) } ") ;
305+ }
306+
307+ return response ;
308+ }
309+ catch ( JsonException )
291310 {
292311 throw new InvalidOperationException (
293- $ "Could not deserialize error from response, response: { await message . Content . ReadAsStringAsync ( ) } ") ;
312+ $ "Deserialize response failed, status code: { message . StatusCode } , Body: { await message . Content . ReadAsStringAsync ( ) } ") ;
294313 }
295-
296- return response ;
297314 }
298315}
0 commit comments