-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
c79a61c
Do not leak exception details in production
elzik 86f0986
Fix unauthorised scenario asserts
elzik a07c208
Fix whitespace issue
elzik d4309f8
Fix whitespace
elzik e765be2
Fix whitespace
elzik 4b34d4b
Ensure tests cover exception handler as well as development excetion …
elzik 9571f3f
Merge branch 'improve-api-error-handling' of https://github.com/elzik…
elzik a0d1d9f
Revert "Ensure tests cover exception handler as well as development e…
elzik 040c833
Use additional class to ensure tests cover exception handler as well …
elzik 12871ac
Use multiple WebApplicationFactories to cover production and deveopme…
elzik 3ef8ba8
Use TheoryData as return type to provide better type safety
elzik 6867e19
Explicit;ly configure exception handler
elzik dcde89c
Ensure WebApplicationFactory and client get disposed
elzik 22ce3e7
Add http sample file to solution
elzik e7f7505
Upgrade to .NET 10 (#172)
elzik c22f15b
Do not leak exception details in production
elzik 65a7f54
Fix unauthorised scenario asserts
elzik 980b709
Fix whitespace
elzik 11c2d94
Fix whitespace
elzik a717e05
Ensure tests cover exception handler as well as development excetion …
elzik 7a85bde
Fix whitespace issue
elzik 27eaba5
Revert "Ensure tests cover exception handler as well as development e…
elzik fa9df67
Use additional class to ensure tests cover exception handler as well …
elzik 2f47678
Use multiple WebApplicationFactories to cover production and deveopme…
elzik 829f415
Use TheoryData as return type to provide better type safety
elzik 539d294
Explicit;ly configure exception handler
elzik 9a806d5
Ensure WebApplicationFactory and client get disposed
elzik 5adc8ed
Add http sample file to solution
elzik 52641f1
Implement ProblemDetails for only caller fixable extensions
elzik bc484ca
Use extension method for exception handling
elzik 3de67a7
Don't end Problem Deatils titles with full-stops
elzik 89c82cf
Refactor deprecated ConfigureWebHost override pattern
elzik 48fe13c
Treat all ICallerFixableException instaces as a Status400BadRequest
elzik 5b08a15
Add funcitonal Problem Details tests
elzik fd38a49
Remove unnecessary null-conditional operator
elzik 329fe4a
Don't perform seperate dev and prod health tests - there is no value
elzik 9193756
Make namespaces clearer
elzik 64344d8
Remove unused WebApplicationFactories
elzik 6448dac
Remove unecessary launch profile
elzik f0862a4
Add trace ID to Problem Details and log
elzik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Elzik.Breef.Api/ExceptionHandling/ExceptionHandlingExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| using Elzik.Breef.Domain; | ||
| using Microsoft.AspNetCore.Diagnostics; | ||
| using Serilog.Context; | ||
| using System.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)) | ||
| { | ||
| 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 | ||
| }; | ||
|
|
||
| if(Activity.Current != null) | ||
| { | ||
| problemDetails.Extensions["traceId"] = Activity.Current.TraceId.ToString(); | ||
| } | ||
|
|
||
| context.Response.StatusCode = statusCode; | ||
| context.Response.ContentType = "application/problem+json"; | ||
| await context.Response.WriteAsJsonAsync(problemDetails); | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/Elzik.Breef.Infrastructure/CallerFixableHttpRequestException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
....Api.Tests.Functional/BreefTestsNative.cs → ...sts.Functional/Breefs/BreefTestsNative.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.