/ApiExceptions

Reinventing C# exceptions

Primary LanguageC#MIT LicenseMIT

Tests

General

This library defines ApiExceptions that you can throw in your .Net applications.

Use with Asp.Net Core

You can always refer to this Asp.Net Core Sample project for guidance.

Setup:

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

Usage:

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:

404-screenshot

Extra:

You can also add any custom fields:

anonymous-screenshot

These responses follow RFC7807: Problem Details standard.

Use outside of Asp.Net Core

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.