This library defines ApiExceptions
that you can throw in your .Net applications.
You can always refer to this Asp.Net Core Sample project for guidance.
Add nuget package:
dotnet add package BitzArt.ApiExceptions.AspNetCore
Add these 2 lines of code to your Program.cs
:
builder.Services.AddApiExceptionHandler(); // When configuring services
app.UseApiExceptionHandler(); // Before controllers
Then, anywhere in your code, you can throw exceptions like:
throw ApiException.NotFound("sample 'not found' message");
This will generate an http response with appropriate status code:
You can also add any custom fields:
These responses follow RFC7807: Problem Details standard.
To use ApiExceptions
in your applications, add this nuget package to your project:
dotnet add package BitzArt.ApiExceptions
The base package contains no handlers, so you will have to implement them yourself.