Skip to content
This repository was archived by the owner on Aug 30, 2025. It is now read-only.

Commit cf42e26

Browse files
committed
feat: Add TimeoutException as default mapped exception to 504 status code
1 parent 21c2e22 commit cf42e26

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

samples/PowerUtils.AspNetCore.ErrorHandler.Samples/Controllers/ExceptionsController.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,9 @@ public IActionResult TestException()
4444
[HttpGet("custom-exception")]
4545
public IActionResult CustomException()
4646
=> throw new CustomException();
47+
48+
[HttpGet("timeout-exception")]
49+
public IActionResult TimeoutException()
50+
=> throw new TimeoutException();
4751
}
4852
}

src/ErrorHandlerOptions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,23 @@ public class ErrorHandlerOptions
3131
public IDictionary<Type, IExceptionMapper> ExceptionMappers { get; set; } = new Dictionary<Type, IExceptionMapper>();
3232

3333
public ErrorHandlerOptions()
34-
=> ExceptionMappers.Add(
34+
{
35+
ExceptionMappers.Add(
3536
typeof(NotImplementedException),
3637
new ExceptionMapper<NotImplementedException>()
3738
{
3839
Handler = (_) => (StatusCodes.Status501NotImplemented, new Dictionary<string, string>())
3940
}
4041
);
42+
43+
ExceptionMappers.Add(
44+
typeof(TimeoutException),
45+
new ExceptionMapper<TimeoutException>()
46+
{
47+
Handler = (_) => (StatusCodes.Status504GatewayTimeout, new Dictionary<string, string>())
48+
}
49+
);
50+
}
4151
}
4252

4353
public static class ErrorHandlerOptionsExtensions

tests/PowerUtils.AspNetCore.ErrorHandler.Tests/Tests/Controllers/ExceptionsControllerTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,28 @@ public async void CustomExcetionWithSpecificTitleAndLink_Request_582()
204204
content.Title.Should()
205205
.Be("CustomTitle");
206206
}
207+
208+
[Fact]
209+
public async void EndpointWithTimeoutException_Request_504StatusCode()
210+
{
211+
// Arrange
212+
var requestUri = "/exceptions/timeout-exception";
213+
var options = _testsFixture.GetService<IOptions<ApiBehaviorOptions>>();
214+
215+
216+
// Act
217+
(var response, var content) = await _testsFixture.Client.SendGetAsync(requestUri);
218+
options.Value.ClientErrorMapping.TryGetValue((int)response.StatusCode, out var clientErrorData);
219+
220+
221+
// Assert
222+
response.ValidateResponse(HttpStatusCode.GatewayTimeout);
223+
224+
content.ValidateContent(
225+
HttpStatusCode.GatewayTimeout,
226+
clientErrorData,
227+
"GET: " + requestUri
228+
);
229+
}
207230
}
208231
}

0 commit comments

Comments
 (0)