-
Notifications
You must be signed in to change notification settings - Fork 0
Improve API Error Handling #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 34 commits
c79a61c
86f0986
a07c208
d4309f8
e765be2
4b34d4b
9571f3f
a0d1d9f
040c833
12871ac
3ef8ba8
6867e19
dcde89c
22ce3e7
e7f7505
c22f15b
65a7f54
980b709
11c2d94
a717e05
7a85bde
27eaba5
fa9df67
2f47678
829f415
539d294
9a806d5
5adc8ed
52641f1
bc484ca
3de67a7
89c82cf
48fe13c
5b08a15
fd38a49
329fe4a
9193756
64344d8
6448dac
f0862a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| using Elzik.Breef.Domain; | ||
| using Microsoft.AspNetCore.Diagnostics; | ||
|
|
||
| namespace Elzik.Breef.Api.ExceptionHandling; | ||
|
|
||
| public static class ExceptionHandlingExtensions | ||
| { | ||
| public static void AddExceptionHandling(this IServiceCollection services) | ||
| { | ||
| services.AddProblemDetails(); | ||
| } | ||
|
|
||
| public static void UseExceptionHandling(this WebApplication app) | ||
| { | ||
| app.UseExceptionHandler(errorApp => | ||
| { | ||
| errorApp.Run(async context => | ||
| { | ||
| var exceptionHandlerFeature = context.Features.Get<IExceptionHandlerFeature>(); | ||
| var exception = exceptionHandlerFeature?.Error; | ||
| int statusCode; | ||
| string title; | ||
| string detail; | ||
|
|
||
| if (exception is ICallerFixableException) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(exception?.Message)) | ||
|
Check warning on line 27 in src/Elzik.Breef.Api/ExceptionHandling/ExceptionHandlingExtensions.cs
|
||
| { | ||
| throw new InvalidOperationException( | ||
| "Caller-fixable exception must have a non-empty message for the caller to fix.", | ||
| exception); | ||
| } | ||
| statusCode = StatusCodes.Status400BadRequest; | ||
| title = "There was a problem with your request"; | ||
| detail = exception.Message; | ||
| } | ||
| else | ||
| { | ||
| statusCode = StatusCodes.Status500InternalServerError; | ||
| title = "An error occurred while processing your request"; | ||
| detail = "Contact your Breef administrator for a solution."; | ||
| } | ||
|
|
||
| var problemDetails = new Microsoft.AspNetCore.Mvc.ProblemDetails | ||
| { | ||
| Status = statusCode, | ||
| Title = title, | ||
| Detail = detail | ||
| }; | ||
|
|
||
| context.Response.StatusCode = statusCode; | ||
| context.Response.ContentType = "application/problem+json"; | ||
| await context.Response.WriteAsJsonAsync(problemDetails); | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Elzik.Breef.Domain | ||
| { | ||
| /// <summary> | ||
| /// Marker interface for exceptions that can be fixed by the requester/user. | ||
| /// </summary> | ||
| public interface ICallerFixableException | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using Elzik.Breef.Domain; | ||
|
|
||
| namespace Elzik.Breef.Infrastructure | ||
| { | ||
| public class CallerFixableHttpRequestException : HttpRequestException, ICallerFixableException | ||
| { | ||
| public CallerFixableHttpRequestException(string message, Exception? innerException = null) | ||
| : base(message, innerException) | ||
| { | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Mvc.Testing; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace Elzik.Breef.Api.Tests.Functional; | ||
|
|
||
| public class DevelopmentWebApplicationFactory : WebApplicationFactory<Program> | ||
| { | ||
| protected override IHost CreateHost(IHostBuilder builder) | ||
| { | ||
| builder.UseEnvironment("Development"); | ||
| return base.CreateHost(builder); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.