Skip to content

Commit f5f9dfe

Browse files
committed
Catch Exceptions and don't serialize JSON when Unauthorized
1 parent 7bad860 commit f5f9dfe

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/Tesla.NET/Requests/HttpClientExtensions.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Tesla.NET.Requests
77
using System.Collections.Generic;
88
using System.IO;
99
using System.Linq;
10+
using System.Net;
1011
using System.Net.Http;
1112
using System.Net.Http.Headers;
1213
using System.Threading;
@@ -412,10 +413,20 @@ private static async Task<IMessageResponse<T>> ReadFailureResponseAsync<T>(
412413
if (responseMessage == null)
413414
throw new ArgumentNullException(nameof(responseMessage));
414415

415-
JObject rawJson =
416-
IsContentJson(responseMessage)
417-
? await ReadJsonAsync(responseMessage, cancellationToken).ConfigureAwait(false)
418-
: null;
416+
JObject rawJson = null;
417+
try
418+
{
419+
// Check the content is JSON, and the response was not Unauthorized as the API returns a Content-Type
420+
// of application/json, but not JSON content.
421+
rawJson =
422+
IsContentJson(responseMessage) && responseMessage.StatusCode != HttpStatusCode.Unauthorized
423+
? await ReadJsonAsync(responseMessage, cancellationToken).ConfigureAwait(false)
424+
: null;
425+
}
426+
catch
427+
{
428+
// ignored to allow the error code to be returned.
429+
}
419430

420431
IMessageResponse<T> response = new MessageResponse<T>(responseMessage.StatusCode, rawJson);
421432
return response;

0 commit comments

Comments
 (0)