Skip to content

Commit 3c98a63

Browse files
committed
Added error handling for invalid json errors, 500 gateway etc., fixes #303
1 parent a2b9453 commit 3c98a63

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

SpotifyAPI.Web/SpotifyWebClient.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ internal class SpotifyWebClient : IClient
1717
private readonly Encoding _encoding = Encoding.UTF8;
1818
private readonly HttpClient _client;
1919

20+
private const string UnknownErrorJson = "{\"error\": { \"status\": 0, \"message\": \"SpotifyAPI.Web - Unkown Spotify Error\" }}";
21+
2022
public SpotifyWebClient(ProxyConfig proxyConfig = null)
2123
{
2224
HttpClientHandler clientHandler = CreateClientHandler(proxyConfig);
@@ -70,13 +72,26 @@ public async Task<Tuple<ResponseInfo, byte[]>> DownloadRawAsync(string url, Dict
7072
public Tuple<ResponseInfo, T> DownloadJson<T>(string url, Dictionary<string, string> headers = null)
7173
{
7274
Tuple<ResponseInfo, string> response = Download(url, headers);
73-
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
75+
try
76+
{
77+
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
78+
}
79+
catch (JsonException)
80+
{
81+
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(UnknownErrorJson, JsonSettings));
82+
}
7483
}
7584

7685
public async Task<Tuple<ResponseInfo, T>> DownloadJsonAsync<T>(string url, Dictionary<string, string> headers = null)
7786
{
78-
Tuple<ResponseInfo, string> response = await DownloadAsync(url, headers).ConfigureAwait(false);
79-
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
87+
Tuple<ResponseInfo, string> response = await DownloadAsync(url, headers).ConfigureAwait(false);try
88+
{
89+
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
90+
}
91+
catch (JsonException)
92+
{
93+
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(UnknownErrorJson, JsonSettings));
94+
}
8095
}
8196

8297
public Tuple<ResponseInfo, string> Upload(string url, string body, string method, Dictionary<string, string> headers = null)
@@ -136,13 +151,27 @@ public async Task<Tuple<ResponseInfo, byte[]>> UploadRawAsync(string url, string
136151
public Tuple<ResponseInfo, T> UploadJson<T>(string url, string body, string method, Dictionary<string, string> headers = null)
137152
{
138153
Tuple<ResponseInfo, string> response = Upload(url, body, method, headers);
139-
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
154+
try
155+
{
156+
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
157+
}
158+
catch (JsonException)
159+
{
160+
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(UnknownErrorJson, JsonSettings));
161+
}
140162
}
141163

142164
public async Task<Tuple<ResponseInfo, T>> UploadJsonAsync<T>(string url, string body, string method, Dictionary<string, string> headers = null)
143165
{
144166
Tuple<ResponseInfo, string> response = await UploadAsync(url, body, method, headers).ConfigureAwait(false);
145-
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
167+
try
168+
{
169+
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(response.Item2, JsonSettings));
170+
}
171+
catch (JsonException)
172+
{
173+
return new Tuple<ResponseInfo, T>(response.Item1, JsonConvert.DeserializeObject<T>(UnknownErrorJson, JsonSettings));
174+
}
146175
}
147176

148177
public void Dispose()

0 commit comments

Comments
 (0)