Skip to content

Add a handler that serializes exceptions to ProblemDetails (JSON) #2

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 4 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ on:
pull_request:
branches:
- master

env:
DOTNET_2_VERSION: 2.1.808
DOTNET_3_VERSION: 3.1.302
DOTNET_5_VERSION: 5.0.100-preview.6.20318.15
BUILD_CONFIGURATION: Release

jobs:
build-and-test:
runs-on: ubuntu-latest
Expand All @@ -18,15 +25,15 @@ jobs:
- uses: actions/checkout@v1

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
uses: coderpatros/setup-dotnet@sxs
with:
dotnet-version: '3.1.100'
dotnet-version: ${{ env.DOTNET_2_VERSION }},${{ env.DOTNET_3_VERSION }},${{ env.DOTNET_5_VERSION }}

- name: Build
run: dotnet build --configuration Release
run: dotnet build --configuration ${{ env.BUILD_CONFIGURATION }}

- name: Unit Test
run: dotnet test --configuration Release
run: dotnet test --configuration ${{ env.BUILD_CONFIGURATION }}

deploy:
runs-on: ubuntu-latest
Expand All @@ -35,17 +42,16 @@ jobs:
- uses: actions/checkout@v1

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
uses: coderpatros/setup-dotnet@sxs
with:
dotnet-version: '3.1.100'
dotnet-version: ${{ env.DOTNET_2_VERSION }},${{ env.DOTNET_3_VERSION }},${{ env.DOTNET_5_VERSION }}

- uses: aarnott/nbgv@v0.3
with:
setAllVars: true
PublicRelease: true

- name: Pack
run: dotnet pack --configuration Release
run: dotnet pack --configuration ${{ env.BUILD_CONFIGURATION }}

- name: Push to feedz.io
run: dotnet nuget push **/*.nupkg -k ${{ secrets.FEEDZ_API_KEY }} -s https://f.feedz.io/forevolve/exception-mapper/nuget/index.json
Expand Down
39 changes: 38 additions & 1 deletion ForEvolve.ExceptionMapper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 16.0.29728.190
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C2DD6A0D-F994-42A4-BAFF-1FB972850229}"
ProjectSection(SolutionItems) = preProject
src\Directory.Build.props = src\Directory.Build.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForEvolve.ExceptionMapper", "src\ForEvolve.ExceptionMapper\ForEvolve.ExceptionMapper.csproj", "{06C42E2C-A4DB-4CA8-870E-EA84727DF28C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F5AE538B-E864-425E-959A-7ED49533482E}"
ProjectSection(SolutionItems) = preProject
test\Directory.Build.props = test\Directory.Build.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForEvolve.ExceptionMapper.Tests", "test\ForEvolve.ExceptionMapper.Tests\ForEvolve.ExceptionMapper.Tests.csproj", "{241913CB-0426-4709-A3CF-00665C400DE5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6EB72298-998C-41BA-8FE1-E3FDD71E5870}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Directory.Build.props = Directory.Build.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForEvolve.ExceptionMapper.CommonExceptions", "src\ForEvolve.ExceptionMapper.CommonExceptions\ForEvolve.ExceptionMapper.CommonExceptions.csproj", "{E3E588AC-0B8B-4007-8680-95DBBC33457A}"
Expand Down Expand Up @@ -50,7 +57,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForEvolve.ExceptionMapper.C
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForEvolve.ExceptionMapper.CommonHttpExceptionHandlers.Tests", "test\ForEvolve.ExceptionMapper.CommonHttpExceptionHandlers.Tests\ForEvolve.ExceptionMapper.CommonHttpExceptionHandlers.Tests.csproj", "{CED04BA2-6293-48D9-92D4-33D58CBB5B56}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi.Shared", "samples\WebApi.Shared\WebApi.Shared.csproj", "{A5051881-D65D-4FB7-9E9C-D10FF48E54E1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.Shared", "samples\WebApi.Shared\WebApi.Shared.csproj", "{A5051881-D65D-4FB7-9E9C-D10FF48E54E1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForEvolve.ExceptionMapper.Serialization.Json", "src\ForEvolve.ExceptionMapper.Serialization.Json\ForEvolve.ExceptionMapper.Serialization.Json.csproj", "{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ForEvolve.ExceptionMapper.Serialization.Json.Tests", "test\ForEvolve.ExceptionMapper.Serialization.Json.Tests\ForEvolve.ExceptionMapper.Serialization.Json.Tests.csproj", "{D0092F11-F801-4C17-AB91-53C060030266}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -290,6 +301,30 @@ Global
{A5051881-D65D-4FB7-9E9C-D10FF48E54E1}.Release|x64.Build.0 = Release|Any CPU
{A5051881-D65D-4FB7-9E9C-D10FF48E54E1}.Release|x86.ActiveCfg = Release|Any CPU
{A5051881-D65D-4FB7-9E9C-D10FF48E54E1}.Release|x86.Build.0 = Release|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Debug|x64.ActiveCfg = Debug|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Debug|x64.Build.0 = Debug|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Debug|x86.ActiveCfg = Debug|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Debug|x86.Build.0 = Debug|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Release|Any CPU.Build.0 = Release|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Release|x64.ActiveCfg = Release|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Release|x64.Build.0 = Release|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Release|x86.ActiveCfg = Release|Any CPU
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37}.Release|x86.Build.0 = Release|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Debug|x64.ActiveCfg = Debug|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Debug|x64.Build.0 = Debug|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Debug|x86.ActiveCfg = Debug|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Debug|x86.Build.0 = Debug|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Release|Any CPU.Build.0 = Release|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Release|x64.ActiveCfg = Release|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Release|x64.Build.0 = Release|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Release|x86.ActiveCfg = Release|Any CPU
{D0092F11-F801-4C17-AB91-53C060030266}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -314,6 +349,8 @@ Global
{E29EF017-7E0B-4F20-A1D3-08FA22EA05AD} = {C2DD6A0D-F994-42A4-BAFF-1FB972850229}
{CED04BA2-6293-48D9-92D4-33D58CBB5B56} = {F5AE538B-E864-425E-959A-7ED49533482E}
{A5051881-D65D-4FB7-9E9C-D10FF48E54E1} = {B236C0BC-2FEF-4F68-8EE3-A6281924D00F}
{FB8BAD2F-F09C-486F-9461-40ECCCB17A37} = {C2DD6A0D-F994-42A4-BAFF-1FB972850229}
{D0092F11-F801-4C17-AB91-53C060030266} = {F5AE538B-E864-425E-959A-7ED49533482E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4D008587-73AC-49F3-A485-00D80DA2B10D}
Expand Down
107 changes: 87 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,33 @@ The packages follows _semantic versioning_. I use `Nerdbank.GitVersioning` to au
Prerelease packages are packaged code not yet merged to `master`.
The prerelease packages are hosted at [feedz.io](feedz.io), thanks to their "Open Source" subscription.

[![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper%2Flatest&label=ForEvolve.ExceptionMapper)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper/latest/download)
[![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper.CommonExceptions%2Flatest&label=ForEvolve.ExceptionMapper.CommonExceptions)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper.CommonExceptions/latest/download)
[![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper.FluentMapper%2Flatest&label=ForEvolve.ExceptionMapper.FluentMapper)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper.FluentMapper/latest/download)

### Released

The packages are published on [NuGet.org](NuGet.org).

[![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper/)
[![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper.CommonExceptions)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper.CommonExceptions/)
[![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper.FluentMapper)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper.FluentMapper/)

> Nothing released yet...
The release and some preview packages are published on [NuGet.org](https://www.nuget.org/).

## How to install

Just load one or more of these NuGet packages:
Load the `ForEvolve.ExceptionMapper` NuGet package or one or more of the following if you prefer loading only part of the _ExceptionMapper_.

```bash
dotnet add package ForEvolve.ExceptionMapper
dotnet add package ForEvolve.ExceptionMapper.CommonExceptions
dotnet add package ForEvolve.ExceptionMapper.FluentMapper
```
**List of packages**

| Name | NuGet.org | feedz.io |
| -------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dotnet add package ForEvolve.ExceptionMapper` | [![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper/) | [![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper%2Flatest)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper/latest/download) |
| `dotnet add package ForEvolve.ExceptionMapper.CommonExceptions` | [![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper.CommonExceptions)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper.CommonExceptions/) | [![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper.CommonExceptions%2Flatest)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper.CommonExceptions/latest/download) |
| `dotnet add package ForEvolve.ExceptionMapper.CommonHttpExceptionHandlers` | [![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper.CommonHttpExceptionHandlers)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper.CommonHttpExceptionHandlers/) | [![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper.CommonHttpExceptionHandlers%2Flatest)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper.CommonHttpExceptionHandlers/latest/download) |
| `dotnet add package ForEvolve.ExceptionMapper.Core` | [![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper.Core)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper.Core/) | [![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper.Core%2Flatest)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper.Core/latest/download) |
| `dotnet add package ForEvolve.ExceptionMapper.FluentMapper` | [![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper.FluentMapper)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper.FluentMapper/) | [![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper.FluentMapper%2Flatest)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper.FluentMapper/latest/download) |
| `dotnet add package ForEvolve.ExceptionMapper.HttpMiddleware` | [![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper.HttpMiddleware)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper.HttpMiddleware/) | [![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper.HttpMiddleware%2Flatest)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper.HttpMiddleware/latest/download) |
| `dotnet add package ForEvolve.ExceptionMapper.Scrutor` | [![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper.Scrutor)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper.Scrutor/) | [![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper.Scrutor%2Flatest)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper.Scrutor/latest/download) |
| `dotnet add package ForEvolve.ExceptionMapper.Serialization.Json` | [![NuGet.org](https://img.shields.io/nuget/vpre/ForEvolve.ExceptionMapper.Serialization.Json)](https://www.nuget.org/packages/ForEvolve.ExceptionMapper.Serialization.Json/) | [![feedz.io](https://img.shields.io/badge/endpoint.svg?url=https%3A%2F%2Ff.feedz.io%2Fforevolve%2Fexception-mapper%2Fshield%2FForEvolve.ExceptionMapper.Serialization.Json%2Flatest)](https://f.feedz.io/forevolve/exception-mapper/packages/ForEvolve.ExceptionMapper.Serialization.Json/latest/download) |

# ForEvolve.ExceptionMapper

This package references the other packages.

# ForEvolve.ExceptionMapper.Core

This package contains the core components like the interfaces, the middleware, etc.
You can load only this package to create your own exceptions and handlers.

Expand Down Expand Up @@ -237,20 +238,86 @@ services.AddExceptionMapper(builder => builder

Under the hood, the `Map<TException>()` extension creates a `FluentExceptionHandler<TException>` that is configurable. You can append, prepend or replace handler actions.

# ForEvolve.ExceptionMapper.Serialization.Json

This package contains a handler that serializes exceptions as `ProblemDetails`.

The following serializes all exception using the default options:

```csharp
services.AddExceptionMapper(builder => builder
// ...
.SerializeAsProblemDetails()
);
```

## Configuring the serialization handler

It is possible to configure the `ProblemDetailsSerializationOptions` like any other options, or using the following extension method:

```csharp
public class Startup
{
public IConfiguration Configuration { get; }
// ...
public void ConfigureServices(IServiceCollection services)
{
services.AddExceptionMapper(builder => builder
// ...
.SerializeAsProblemDetails(Configuration.GetSection("ExceptionMapper"))
);
}
// ...
}
```

Here is an example of `appsettings.json`:

```json
{
"ExceptionMapper": {
"SerializeExceptions": true,
"ContentType": "application/problem+json",
"JsonSerializerOptions": {
"IgnoreNullValues": true
// ...
}
}
}
```

Another overload is to pass an instance of `ProblemDetailsSerializationOptions` directly like this:

```csharp
public void ConfigureServices(IServiceCollection services)
{
var options = new ProblemDetailsSerializationOptions();
services.AddExceptionMapper(builder => builder
// ...
.SerializeAsProblemDetails(options)
);
}
```

# Release notes

## 1.1.0

- Add a handler that serializes exceptions to `ProblemDetails` (JSON)
- Add the `ForEvolve.ExceptionMapper.Serialization.Json` project

## 1.0.0

- Initial release
- Initial release (no yet released)

# Future/To do

Here is a list of what I want to do:

- [x] Take the fallback out of `MapCommonHttpExceptions()` into its own extension, like `MapHttpFallback()`
- [ ] Create a MVC/Razor Pages filter that could replace the middleware or work in conjunction of it, adding more control over the process for MVC applications.
- [ ] Add one or more serialization handlers that at least support JSON serialization and that leverage `ProblemDetailsFactory` to create `ProblemDetails` objects.
- [x] Add one or more serialization handlers that at least support JSON serialization and that leverage `ProblemDetailsFactory` to create `ProblemDetails` objects.
- [ ] Write tests that covers `ForEvolve.ExceptionMapper.FluentMapper` and other missing pieces
- [ ] Create a MVC/Razor Pages filter that could replace the middleware or work in conjunction of it, adding more control over the process for MVC applications. _This may not be important._

# Found a bug or have a feature request?

Expand Down
Loading