Skip to content

Commit e116e31

Browse files
authored
Fix: expand error handling in GetTimeSeries() (#13)
* feat: expand error handling in GetTimeSeries * remove: case "application/x-download"
1 parent 1a288a9 commit e116e31

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

QuantConnect.AlphaVantage/AlphaVantageDataDownloader.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,28 @@ private IEnumerable<TimeSeries> GetTimeSeries(RestRequest request)
272272
Log.Trace("Downloading /{0}?{1}", request.Resource, string.Join("&", request.Parameters.Where(p => p.Name != "apikey")));
273273
var response = _avClient.Get(request);
274274

275-
if (response.ContentType != "application/x-download")
275+
switch (response.ContentType)
276276
{
277-
throw new FormatException($"Unexpected content received from API.\n{response.Content}");
277+
case "application/json":
278+
var jObject = JObject.Parse(response.Content);
279+
280+
if (jObject.TryGetValue("Information", StringComparison.InvariantCultureIgnoreCase, out var infoToken))
281+
{
282+
throw new Exception($"{nameof(AlphaVantageDataDownloader)}.{nameof(GetTimeSeries)}.Information: {infoToken}");
283+
}
284+
285+
if (jObject.TryGetValue("Error Message", StringComparison.InvariantCultureIgnoreCase, out var errorToken))
286+
{
287+
var errorMessage = errorToken?.ToString();
288+
if (!string.IsNullOrEmpty(errorMessage) &&
289+
errorMessage.Contains("Please retry", StringComparison.InvariantCultureIgnoreCase))
290+
{
291+
Log.Trace($"Received an error from Alpha Vantage: \"{errorMessage}\". Retrying the request...");
292+
return GetTimeSeries(request);
293+
}
294+
throw new Exception($"{nameof(AlphaVantageDataDownloader)}.{nameof(GetTimeSeries)}.Error Message: {errorMessage}");
295+
}
296+
throw new Exception($"{nameof(AlphaVantageDataDownloader)}.{nameof(GetTimeSeries)}.Unexpected JSON Response: {response.Content}");
278297
}
279298

280299
using (var reader = new StringReader(response.Content))

0 commit comments

Comments
 (0)