Throw exceptions instead of trying to recover
SuccessfulCode/BritishHorseRacingApi/master This commit looks good

This commit is contained in:
2020-04-12 18:01:14 +01:00
parent b4cf9d0056
commit b851cde957
2 changed files with 40 additions and 14 deletions
+15 -14
View File
@@ -47,11 +47,11 @@ namespace BritishHorseRacingApi
var response = await _client.SendAsync(message);
if (!response.IsSuccessStatusCode)
{
throw new Exception("Token not retrieved");
throw new ApiResponseException("Token not retrieved:" + 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));
if (content.StartsWith("<"))
@@ -64,10 +64,9 @@ namespace BritishHorseRacingApi
}
response = await _client.SendAsync(message);
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));
Trace.WriteLine("Waiting after conntect fail");
throw new ApiResponseException("Failed to get token. Response: " + content);
}
}
@@ -262,16 +261,18 @@ namespace BritishHorseRacingApi
}
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);
if (!content.StartsWith("<"))
{
await Task.Delay(TimeSpan.FromSeconds(30));
response = await SendRequest(endpoint);
content = response.Content;
continue;
}
await Task.Delay(TimeSpan.FromSeconds(_waitTimeAfterFail));
if (content.StartsWith("<"))
await GetNewToken();
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 GetNewToken();
@@ -285,7 +286,7 @@ namespace BritishHorseRacingApi
if (!response.IsSuccessStatusCode)
{
throw new Exception($"Unexpected error: {content}" );
throw new ApiResponseException($"Unexpected error: {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)
{
}
}
}