Skip to content

Commit

Permalink
Merge 52b13ba into 53e325d
Browse files Browse the repository at this point in the history
  • Loading branch information
andreloureiro88 committed Mar 18, 2022
2 parents 53e325d + 52b13ba commit d0604f5
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/Castle.Sdk/Actions/Authenticate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ internal static class Authenticate
{
return await send();
}
catch (CastleNotFoundException e)
{
throw e;
}
catch (Exception e)
{
logger.Warn(() => "Failover, " + e);
Expand All @@ -47,6 +51,14 @@ private static Verdict CreateFailoverResponse(ActionType strategy, string reason

private static Verdict CreateFailoverResponse(ActionType strategy, Exception exception)
{
if (exception is CastleInvalidTokenException)
{
return CreateFailoverResponse(ActionType.Deny, exception.Message);
}
if (exception is CastleInvalidParametersException)
{
return CreateFailoverResponse(strategy, exception.Message);
}
return CreateFailoverResponse(strategy, exception is CastleTimeoutException ? "timeout" : "server error");
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Castle.Sdk/Actions/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ internal static class Filter
{
return await send();
}
catch (CastleNotFoundException e)
{
throw e;
}
catch (Exception e)
{
logger.Warn(() => "Failover, " + e);
Expand All @@ -43,6 +47,14 @@ private static RiskResponse CreateFailoverResponse(ActionType strategy, string r

private static RiskResponse CreateFailoverResponse(ActionType strategy, Exception exception)
{
if (exception is CastleInvalidTokenException)
{
return CreateFailoverResponse(ActionType.Deny, exception.Message);
}
if (exception is CastleInvalidParametersException)
{
return CreateFailoverResponse(strategy, exception.Message);
}
return CreateFailoverResponse(strategy, exception is CastleTimeoutException ? "timeout" : "server error");
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Castle.Sdk/Actions/Risk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ internal static class Risk
{
return await send();
}
catch (CastleNotFoundException e)
{
throw e;
}
catch (Exception e)
{
logger.Warn(() => "Failover, " + e);
Expand All @@ -43,6 +47,14 @@ private static RiskResponse CreateFailoverResponse(ActionType strategy, string r

private static RiskResponse CreateFailoverResponse(ActionType strategy, Exception exception)
{
if (exception is CastleInvalidTokenException)
{
return CreateFailoverResponse(ActionType.Deny, exception.Message);
}
if (exception is CastleInvalidParametersException)
{
return CreateFailoverResponse(strategy, exception.Message);
}
return CreateFailoverResponse(strategy, exception is CastleTimeoutException ? "timeout" : "server error");
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Castle.Sdk/Castle.Sdk.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Castle.Sdk/Infrastructure/ExceptionGuard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ internal static class ExceptionGuard
{
throw;
}
catch (CastleNotFoundException e)
{
throw e;
}
catch (Exception e)
{
logger.Error(e.ToString);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Net;

namespace Castle.Infrastructure.Exceptions
{
/// <summary>
/// Exception for Unprocessable Entity
/// </summary>
internal class CastleInvalidParametersException : Exception
{
public CastleInvalidParametersException(string message, string requestUri, HttpStatusCode? httpStatusCode = null)
: base(message)
{
HttpStatusCode = httpStatusCode;
RequestUri = requestUri;
}

public HttpStatusCode? HttpStatusCode { get; set; }

public string RequestUri { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Net;

namespace Castle.Infrastructure.Exceptions
{
/// <summary>
/// Exception for Invalid request token
/// </summary>
internal class CastleInvalidTokenException : Exception
{
public CastleInvalidTokenException(string message, string requestUri, HttpStatusCode? httpStatusCode = null)
: base(message)
{
HttpStatusCode = httpStatusCode;
RequestUri = requestUri;
}

public HttpStatusCode? HttpStatusCode { get; set; }

public string RequestUri { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Net;

namespace Castle.Infrastructure.Exceptions
{
/// <summary>
/// Exception for route not found
/// </summary>
internal class CastleNotFoundException : Exception
{
public CastleNotFoundException(string message, string requestUri, HttpStatusCode? httpStatusCode = null)
: base(message)
{
HttpStatusCode = httpStatusCode;
RequestUri = requestUri;
}

public HttpStatusCode? HttpStatusCode { get; set; }

public string RequestUri { get; set; }
}
}
37 changes: 35 additions & 2 deletions src/Castle.Sdk/Infrastructure/Extensions/HttpResponseExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
using System.Net.Http;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Castle.Infrastructure.Exceptions;
using Castle.Infrastructure.Json;

namespace Castle.Infrastructure.Extensions
{
internal static class HttpResponseExtensions
{
public static async Task<CastleInternalException> ToCastleException(this HttpResponseMessage message, string requestUri)
public static async Task<Exception> ToCastleException(this HttpResponseMessage message, string requestUri)
{

if (message.StatusCode == HttpStatusCode.NotFound)
{
throw new CastleNotFoundException("Not Found", requestUri, message.StatusCode);
}
var content = await message.Content.ReadAsStringAsync();
try
{
var parsedContent = JsonForCastle.DeserializeObject<Dictionary<string, string>>(content);
if (parsedContent["type"] != null)
{
if (parsedContent["type"] == "invalid_request_token")
{
return new CastleInvalidTokenException(parsedContent["message"], requestUri, message.StatusCode);
}
return new CastleInvalidParametersException(parsedContent["message"], requestUri, message.StatusCode);
}
}
catch (JsonException)
{
return new CastleInternalException(content, requestUri, message.StatusCode);
}
catch (Exception)
{
return new CastleInvalidParametersException(content, requestUri, message.StatusCode);
}



return new CastleInternalException(content, requestUri, message.StatusCode);
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/Tests/Sending/When_converting_http_messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public void Should_create_stringcontent_from_payload(object payload)
public async Task Should_create_exception_from_httpresponse(HttpResponseMessage response, string uri)
{
var result = await response.ToCastleException(uri);

result.HttpStatusCode.Should().Be(response.StatusCode);
result.RequestUri.Should().Be(uri);
result.Message.Should().Be(await response.Content.ReadAsStringAsync());
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Tests/Sending/When_sending_requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class When_sending_requests
.Received()
.Info(Arg.Is<Func<string>>(func => func()
.StartsWith("Response")));
}
}
}

[Theory, AutoFakeData(typeof(HttpMessageHandlerSuccessCustomization))]
Expand All @@ -62,7 +62,7 @@ public class When_sending_requests
{
var result = await testMethod(sut);
result.Should().BeOfType<VoidResponse>();
}
}
}

[Theory, AutoFakeData(typeof(HttpMessageHandlerFailureCustomization))]
Expand All @@ -76,8 +76,8 @@ public class When_sending_requests
foreach (var testMethod in TestMethods)
{
Func<Task> act = async () => await testMethod(sut);
act.Should().Throw<CastleInternalException>();
}
act.Should().Throw<CastleInvalidParametersException>();
}
}

[Theory, AutoFakeData(typeof(HttpMessageHandlerCancelledCustomization))]
Expand All @@ -92,7 +92,7 @@ public class When_sending_requests
{
Func<Task> act = async () => await testMethod(sut);
act.Should().Throw<CastleTimeoutException>();
}
}
}

private static readonly Func<HttpMessageSender, Task<VoidResponse>>[] TestMethods =
Expand Down

0 comments on commit d0604f5

Please sign in to comment.