Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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...

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);
}
}
```