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()