From c74105d882c218e0e93338bc9734d848289abbda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Pawelec?= Date: Wed, 29 Nov 2023 12:06:36 +0100 Subject: [PATCH] return 524 status code on server side timeout. --- CHANGELOG.md | 2 ++ .../ExceptionHandlingMiddleware.cs | 2 +- .../Exceptions/InternalServerException.cs | 11 ----------- .../Exceptions/RequestTimeoutException.cs | 16 ++++++++++++++++ 4 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 src/RadixDlt.NetworkGateway.GatewayApi/Exceptions/RequestTimeoutException.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 928fd7e6e..59251cd78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ Release Date: _unreleased_ - Fixed exception thrown on empty validator set in the `/state/validator/list` endpoint. +- `524` status code returned instead of `500` if request takes longer than configured timeout. + ## 1.2.2 Release Date: 22.11.2023 diff --git a/apps/GatewayApi/ExceptionHandlingMiddleware/ExceptionHandlingMiddleware.cs b/apps/GatewayApi/ExceptionHandlingMiddleware/ExceptionHandlingMiddleware.cs index 7b56f4e84..8bd73ea1a 100644 --- a/apps/GatewayApi/ExceptionHandlingMiddleware/ExceptionHandlingMiddleware.cs +++ b/apps/GatewayApi/ExceptionHandlingMiddleware/ExceptionHandlingMiddleware.cs @@ -142,7 +142,7 @@ private KnownGatewayErrorException HandleException(HttpContext httpContext, Exce if (requestTimeoutFeature?.CancellationToken.IsCancellationRequested == true) { _logger.LogError(exception, "Request timed out after={Timeout} seconds, [RequestTrace={TraceId}]", requestTimeoutFeature.Timeout.TotalSeconds, traceId); - return InternalServerException.OfRequestTimeoutException(exception, traceId); + return new RequestTimeoutException(traceId, $"Request timed out after={requestTimeoutFeature.Timeout.TotalSeconds} seconds"); } if (requestAbortedFeature?.CancellationToken.IsCancellationRequested == true) diff --git a/src/RadixDlt.NetworkGateway.GatewayApi/Exceptions/InternalServerException.cs b/src/RadixDlt.NetworkGateway.GatewayApi/Exceptions/InternalServerException.cs index cfa95474d..da9625bf1 100644 --- a/src/RadixDlt.NetworkGateway.GatewayApi/Exceptions/InternalServerException.cs +++ b/src/RadixDlt.NetworkGateway.GatewayApi/Exceptions/InternalServerException.cs @@ -84,17 +84,6 @@ public static InternalServerException OfShareableException(Exception exception, ); } - public static InternalServerException OfRequestTimeoutException(Exception exception, string traceId) - { - var message = $"Request timed out. If reporting this issue, please include TraceId={traceId}"; - - return new InternalServerException( - new GatewayModel.InternalServerError(exception.GetType().Name, exception.Message), - message, - exception.Message - ); - } - public static InternalServerException OfHiddenException(Exception exception, string traceId) { var message = $"An unexpected error occurred handling the request. If reporting this issue, please include TraceId={traceId}"; diff --git a/src/RadixDlt.NetworkGateway.GatewayApi/Exceptions/RequestTimeoutException.cs b/src/RadixDlt.NetworkGateway.GatewayApi/Exceptions/RequestTimeoutException.cs new file mode 100644 index 000000000..e37e18359 --- /dev/null +++ b/src/RadixDlt.NetworkGateway.GatewayApi/Exceptions/RequestTimeoutException.cs @@ -0,0 +1,16 @@ +namespace RadixDlt.NetworkGateway.GatewayApi.Exceptions; + +public sealed class RequestTimeoutException : KnownGatewayErrorException +{ + /// + /// Non standard status code (there's no suitable one for such situation). + /// In cloudflare means that the server received complete request but it took to long to process on server side. + /// Wanted to filter these cases out from standard internal server errors. + /// + private const int RequestTimeoutHttpStatusCode = 524; + + public RequestTimeoutException(string traceId, string internalMessage) + : base(RequestTimeoutHttpStatusCode, null, $"Request timed out. If reporting this issue, please include TraceId={traceId}", internalMessage) + { + } +}