Throw exceptions instead of trying to recover
SuccessfulCode/BritishHorseRacingApi/master This commit looks good
SuccessfulCode/BritishHorseRacingApi/master This commit looks good
This commit is contained in:
@@ -47,11 +47,11 @@ namespace BritishHorseRacingApi
|
|||||||
var response = await _client.SendAsync(message);
|
var response = await _client.SendAsync(message);
|
||||||
if (!response.IsSuccessStatusCode)
|
if (!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
throw new Exception("Token not retrieved");
|
throw new ApiResponseException("Token not retrieved:" + response.Content);
|
||||||
}
|
}
|
||||||
|
|
||||||
var content = response.Content;
|
var content = response.Content;
|
||||||
while (content.StartsWith("<") || content.StartsWith("Error: \"Failed to connect"))
|
if (content.StartsWith("<") || content.StartsWith("Error: \"Failed to connect"))
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromSeconds(_waitTimeAfterFail));
|
await Task.Delay(TimeSpan.FromSeconds(_waitTimeAfterFail));
|
||||||
if (content.StartsWith("<"))
|
if (content.StartsWith("<"))
|
||||||
@@ -64,10 +64,9 @@ namespace BritishHorseRacingApi
|
|||||||
}
|
}
|
||||||
response = await _client.SendAsync(message);
|
response = await _client.SendAsync(message);
|
||||||
content = response.Content;
|
content = response.Content;
|
||||||
if (content.StartsWith("Error: \"Failed to connect"))
|
if (content.StartsWith("<") || content.StartsWith("Error: \"Failed to connect"))
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromSeconds(30));
|
throw new ApiResponseException("Failed to get token. Response: " + content);
|
||||||
Trace.WriteLine("Waiting after conntect fail");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,16 +261,18 @@ namespace BritishHorseRacingApi
|
|||||||
}
|
}
|
||||||
|
|
||||||
var content = response.Content;
|
var content = response.Content;
|
||||||
while (content.StartsWith("<") || content.StartsWith("Error: \"Failed to connect"))
|
if(content.StartsWith("<") || content.StartsWith("Error: \"Failed to connect"))
|
||||||
{
|
{
|
||||||
Trace.WriteLine("Content not valid requerying. Content: "+ content);
|
Trace.WriteLine("Content not valid requerying. Content: "+ content);
|
||||||
if (!content.StartsWith("<"))
|
await Task.Delay(TimeSpan.FromSeconds(_waitTimeAfterFail));
|
||||||
{
|
|
||||||
await Task.Delay(TimeSpan.FromSeconds(30));
|
if (content.StartsWith("<"))
|
||||||
response = await SendRequest(endpoint);
|
await GetNewToken();
|
||||||
content = response.Content;
|
|
||||||
continue;
|
response = await SendRequest(endpoint);
|
||||||
}
|
content = response.Content;
|
||||||
|
if (content.StartsWith("<") || content.StartsWith("Error: \"Failed to connect"))
|
||||||
|
throw new ApiResponseException($"Error in response: {content}");
|
||||||
|
|
||||||
await Task.Delay(TimeSpan.FromSeconds(_waitTimeAfterFail));
|
await Task.Delay(TimeSpan.FromSeconds(_waitTimeAfterFail));
|
||||||
await GetNewToken();
|
await GetNewToken();
|
||||||
@@ -285,7 +286,7 @@ namespace BritishHorseRacingApi
|
|||||||
|
|
||||||
if (!response.IsSuccessStatusCode)
|
if (!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
throw new Exception($"Unexpected error: {content}" );
|
throw new ApiResponseException($"Unexpected error: {content}" );
|
||||||
}
|
}
|
||||||
|
|
||||||
if(preDeserilizationValidation != null && !preDeserilizationValidation(JToken.Parse(content)))
|
if(preDeserilizationValidation != null && !preDeserilizationValidation(JToken.Parse(content)))
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace BritishHorseRacingApi
|
||||||
|
{
|
||||||
|
[Serializable]
|
||||||
|
internal class ApiResponseException : Exception
|
||||||
|
{
|
||||||
|
public ApiResponseException()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiResponseException(string message) : base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApiResponseException(string message, Exception innerException) : base(message, innerException)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ApiResponseException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user