Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/laget-se/laget.exceptions
A generic implementation of Exceptions used in our applications...
https://github.com/laget-se/laget.exceptions
nuget
Last synced: 22 days ago
JSON representation
A generic implementation of Exceptions used in our applications...
- Host: GitHub
- URL: https://github.com/laget-se/laget.exceptions
- Owner: laget-se
- License: apache-2.0
- Created: 2020-12-17T11:30:16.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-03T06:39:52.000Z (7 months ago)
- Last Synced: 2024-06-03T08:10:05.794Z (7 months ago)
- Topics: nuget
- Language: C#
- Homepage:
- Size: 80.1 KB
- Stars: 0
- Watchers: 7
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Exceptions
A generic implementation of Exceptions used in our applications...![Nuget](https://img.shields.io/nuget/v/laget.Exceptions)
![Nuget](https://img.shields.io/nuget/dt/laget.Exceptions)## Usage
```c#
public class ConstraintException : laget.Exceptions.Exception
{
public override HttpStatusCode StatusCode => HttpStatusCode.MethodNotAllowed;public ConstraintException(string message)
: base(message)
{
}public ConstraintException(string message, Exception ex)
: base(message, ex)
{
}
}
```### Middleware
```c#
public class ExceptionMiddleware
{
readonly RequestDelegate _next;public ExceptionMiddleware(RequestDelegate next)
{
_next = next;
}public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (laget.Exceptions.Exception ex)
{
await HandleExceptionAsync(context, ex);
}
catch (Exception e)
{
if (e.InnerException?.GetBaseException() is laget.Exceptions.Exception)
{
await HandleExceptionAsync(context, e.InnerException?.GetBaseException() as laget.Exceptions.Exception);
}await HandleExceptionAsync(context, e);
}
}static Task HandleExceptionAsync(HttpContext context, Exception ex)
{
return HandleExceptionAsync(context, ex.GetResponse());
}static Task HandleExceptionAsync(HttpContext context, laget.Exceptions.Exception ex)
{
return HandleExceptionAsync(context, ex.GetResponse());
}static Task HandleExceptionAsync(HttpContext context, laget.Exceptions.Models.Response model)
{
context.Response.Clear();
context.Response.ContentType = "application/problem+json";
context.Response.StatusCode = model.Status ?? (int)HttpStatusCode.InternalServerError;var response = JsonConvert.SerializeObject(model);
return context.Response.WriteAsync(response);
}
}
```