From 3c98a63873f7375a12fa7f4fbe87d03608ebf6aa Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Sat, 22 Dec 2018 21:40:08 +0100 Subject: [PATCH] Added error handling for invalid json errors, 500 gateway etc., fixes #303 --- SpotifyAPI.Web/SpotifyWebClient.cs | 39 ++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/SpotifyAPI.Web/SpotifyWebClient.cs b/SpotifyAPI.Web/SpotifyWebClient.cs index 4812bceb..65e829ea 100644 --- a/SpotifyAPI.Web/SpotifyWebClient.cs +++ b/SpotifyAPI.Web/SpotifyWebClient.cs @@ -17,6 +17,8 @@ namespace SpotifyAPI.Web private readonly Encoding _encoding = Encoding.UTF8; private readonly HttpClient _client; + private const string UnknownErrorJson = "{\"error\": { \"status\": 0, \"message\": \"SpotifyAPI.Web - Unkown Spotify Error\" }}"; + public SpotifyWebClient(ProxyConfig proxyConfig = null) { HttpClientHandler clientHandler = CreateClientHandler(proxyConfig); @@ -70,13 +72,26 @@ namespace SpotifyAPI.Web public Tuple DownloadJson(string url, Dictionary headers = null) { Tuple response = Download(url, headers); - return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); + try + { + return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); + } + catch (JsonException) + { + return new Tuple(response.Item1, JsonConvert.DeserializeObject(UnknownErrorJson, JsonSettings)); + } } public async Task> DownloadJsonAsync(string url, Dictionary headers = null) { - Tuple response = await DownloadAsync(url, headers).ConfigureAwait(false); - return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); + Tuple response = await DownloadAsync(url, headers).ConfigureAwait(false);try + { + return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); + } + catch (JsonException) + { + return new Tuple(response.Item1, JsonConvert.DeserializeObject(UnknownErrorJson, JsonSettings)); + } } public Tuple Upload(string url, string body, string method, Dictionary headers = null) @@ -136,13 +151,27 @@ namespace SpotifyAPI.Web public Tuple UploadJson(string url, string body, string method, Dictionary headers = null) { Tuple response = Upload(url, body, method, headers); - return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); + try + { + return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); + } + catch (JsonException) + { + return new Tuple(response.Item1, JsonConvert.DeserializeObject(UnknownErrorJson, JsonSettings)); + } } public async Task> UploadJsonAsync(string url, string body, string method, Dictionary headers = null) { Tuple response = await UploadAsync(url, body, method, headers).ConfigureAwait(false); - return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); + try + { + return new Tuple(response.Item1, JsonConvert.DeserializeObject(response.Item2, JsonSettings)); + } + catch (JsonException) + { + return new Tuple(response.Item1, JsonConvert.DeserializeObject(UnknownErrorJson, JsonSettings)); + } } public void Dispose()