You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple ASP.NET Core middleware that intercepts and reacts to `Exception`.
8
-
You can map specific exception types to HTTP Status Code, use predefined handlers, or create your own.
8
+
You can map specific exception types to HTTP Status Codes, use predefined handlers, or create your own.
9
9
10
10
You can throw an exception from anywhere in your codebase and ExceptionMapper will handle it according to your specifications.
11
11
This makes it a breeze to uniformize exception handling in a REST API.
12
12
13
-
All of the handlers are iterated through, in order, so you can build a pipeline to handle exceptions where multiple handlers have a single responsibility.
13
+
All the handlers are iterated through, in order, so you can build a pipeline to handle exceptions where multiple handlers have a single responsibility.
14
14
For example, you could have handlers that respond to certain exception types, then one or more fallback handlers that react only if no previous handler handled the exception.
15
15
16
-
Finally, there is a serializer that convert handled exceptions to JSON, in the format of your choice, making your API linear between endpoints and exception types without much effort. The default serializer converts the errors to [Problem Details for HTTP APIs](https://datatracker.ietf.org/doc/html/rfc7807).
17
-
18
-
## Versioning
19
-
20
-
The packages follows _semantic versioning_ and use `Nerdbank.GitVersioning` to automatically version packages based on git commits.
21
-
22
-
### Pre-released
23
-
24
-
Prerelease packages are packaged code not yet merged to `master`.
25
-
The prerelease CI builds are packaged and hosted at [feedz.io](feedz.io), thanks to their "Open Source" subscription.
16
+
Finally, there is a serializer that converts handled exceptions to JSON, in the format of your choice, making your API linear between endpoints and exception types without much effort. The default serializer converts the errors to [Problem Details for HTTP APIs](https://datatracker.ietf.org/doc/html/rfc7807).
26
17
27
18
## How to install
28
19
@@ -36,7 +27,7 @@ _You can take a look at the `samples/WebApiSample` project for a working example
36
27
37
28
## Getting started
38
29
39
-
You must register the services, and optionally configure/register handlers, and use the middleware that catches exceptions (and that handles the logic).
30
+
You must register the services, optionally configure/register handlers, and use the middleware that catches exceptions (and that handles the logic).
40
31
41
32
**Program.cs**
42
33
@@ -76,7 +67,7 @@ public class Startup
76
67
77
68
## Extending the existing exception
78
69
79
-
An easy way to manage your custom exceptions is to inherit from the one that are already mapped.
70
+
An easy way to manage your custom exceptions is to inherit from the ones that are already mapped.
80
71
For example, you could create and throw the following `DroidNotFoundException` and ExceptionMapper will associate it with a 404 Not Found status code because it inherits from `NotFoundException`:
81
72
82
73
```csharp
@@ -91,7 +82,7 @@ public class DroidNotFoundException : NotFoundException
91
82
92
83
## Mapping Exception types to status code
93
84
94
-
If you do not want or can't inherit the provided exceptions, you can map any Exception types to specific status code, like this:
85
+
If you do not want or can't inherit the provided exceptions, you can map any Exception types to a specific status code, like this:
95
86
96
87
```csharp
97
88
builder.AddExceptionMapper(builder=>
@@ -188,7 +179,7 @@ ExceptionMapper implements different common exceptions and their handlers, like
188
179
189
180
# Fallback handler
190
181
191
-
ExceptionMapper also comes with a fallback handler that convert unhandled exceptions to `500 InternalServerError`. This is an opt-out feature, configured by the `FallbackExceptionHandlerOptions`.
182
+
ExceptionMapper also comes with a fallback handler that converts unhandled exceptions to `500 InternalServerError`. This is an opt-out feature, configured by the `FallbackExceptionHandlerOptions`.
192
183
193
184
You can also configure the `FallbackExceptionHandlerOptions` like the following or under the `ExceptionMapper:FallbackExceptionHandler` key in your settings:
194
185
@@ -234,11 +225,11 @@ You can also customize the options from the `appsettings.json` file:
234
225
```
235
226
236
227
Note that the serializer displays the debug information when in development.
237
-
Use the `DisplayDebugInformation` function to display the debug info in other environment, like staging or production.
228
+
Use the `DisplayDebugInformation` function to display the debug info in other environments, like staging or production.
238
229
239
230
## Property names
240
231
241
-
To change the way the property name are serialized, you can configure the `JsonOptions` and change the `PropertyNamingPolicy` property.
232
+
To change the way the property names are serialized, you can configure the `JsonOptions` and change the `PropertyNamingPolicy` property.
For .NET 7+ projects, ExceptionMapper sets the `DictionaryKeyPolicy` property to the `PropertyNamingPolicy` property value so dictionaries are serialized the same way as the normal properties.
252
+
For .NET 7+ projects, ExceptionMapper sets the `DictionaryKeyPolicy` property to the `PropertyNamingPolicy` property value, so dictionaries are serialized the same way as the normal properties.
The package follows _semantic versioning_ and uses `Nerdbank.GitVersioning` to automatically version packages based on git commits.
286
+
287
+
### Pre-released
288
+
289
+
Prerelease packages are packaged code not yet merged to the `main` branch.
290
+
The prerelease CI builds are packaged and hosted at [feedz.io](feedz.io), thanks to their "Open Source" subscription.
291
+
292
292
# Release notes
293
293
294
294
## 3.0
295
295
296
-
The version 3 of ExceptionMapper is a major rewrite that simplifies the codebase and usage of the library. Here are a few important changes:
296
+
Version 3 of ExceptionMapper is a major rewrite that simplifies the codebase and usage of the library. Here are a few important changes:
297
297
298
298
- Add support to .NET 7 and .NET 8.
299
299
- Remove transitive dependency on JSON.NET (`Newtonsoft.Json`).
300
300
- Drop support for .NET Standard 2.0 because `ExceptionMapper` depends on the `HttpContext` class which requires a `<FrameworkReference Include="Microsoft.AspNetCore.App" />` which is not compatible with `netstandard2.0`.
301
-
- Merge all assemblies in `ForEvolve.ExceptionMapper` but `ForEvolve.ExceptionMapper.Scrutor` and removed `ForEvolve.ExceptionMapper.Scrutor`althogether.
301
+
- Merge all assemblies in `ForEvolve.ExceptionMapper` but `ForEvolve.ExceptionMapper.Scrutor` and removed `ForEvolve.ExceptionMapper.Scrutor`altogether.
302
302
- Replace the `AddMvcCore` call by registering a copy of the `DefaultProblemDetailsFactory` using a `TryAddSingleton` call, so you must register your custom `ProblemDetailsFactory` implementation before `AddExceptionMapper`. The good news is, if you are using a custom factory, the `ProblemDetailsSerializationHandler` will use it!
303
303
> Removing the copy of the `DefaultProblemDetailsFactory` class could be resolved by https://github.com/dotnet/aspnetcore/issues/49982
304
304
- Calling `AddExceptionMapper()` now registers the common exceptions and the serializer automatically.
305
305
- The `Order` property was removed from the `IExceptionHandler` interface. The system uses the registration order instead.
306
-
- The interface now leverage a serialzier implementing the `IExceptionSerializer` interface. The serializer no longer implements the `IExceptionHandler` interface.
306
+
- The interface now leverages a serializer implementing the `IExceptionSerializer` interface. The serializer no longer implements the `IExceptionHandler` interface.
307
307
- By default, `ProblemDetailsSerializationOptions` is bound to the section `ExceptionMapper:ProblemDetailsSerialization` and `FallbackExceptionHandlerOptions` is bound to the section `ExceptionMapper:FallbackExceptionHandler`.
308
308
309
309
### Breaking changes .NET 7+
310
310
311
311
- Remove the `ContentType` and `JsonSerializerOptions` properties from the `ProblemDetailsSerializationOptions` class (`ForEvolve.ExceptionMapper.Serialization.Json`).
312
-
- The `ProblemDetailsSerializationHandler` class now leverages the `IProblemDetailsService` interface to write the `ProblemDetails` object to the response stream instead of serializing it with the `JsonSerializer`, relinguishing the control of the process to .NET.
312
+
- The `ProblemDetailsSerializationHandler` class now leverages the `IProblemDetailsService` interface to write the `ProblemDetails` object to the response stream instead of serializing it with the `JsonSerializer`, relinquishing the control of the process to .NET.
313
313
- The `ProblemDetailsSerializationHandler` leverages the `JsonOptions` class to ensure the names are formatted according to the `PropertyNamingPolicy` object. The default is `camelCase`.
314
314
- ExceptionMapper sets the `DictionaryKeyPolicy` property to the `PropertyNamingPolicy` property value so dictionaries are serialized the same way as the normal properties.
315
315
316
-
## 2.0
316
+
## 2.0 (deprecated)
317
317
318
318
- Drop .NET Core 3.1 support
319
319
- Add support for .NET 6.0
320
320
321
-
## 1.1
321
+
## 1.1 (deprecated)
322
322
323
323
- Add a handler that serializes exceptions to `ProblemDetails` (JSON)
324
324
- Add the `ForEvolve.ExceptionMapper.Serialization.Json` project
325
325
326
-
## 1.0
327
-
328
-
- Initial release (no yet released)
329
-
330
-
# Future/To do
331
-
332
-
Here is a list of what I want to do:
326
+
## 1.0 (deprecated)
333
327
334
-
-[ ] Improve overall test coverage.
335
-
-[ ] Implement custom type converter. The serializer would either use the converter or fall back to the reflection-based code if no type converter is available.
336
-
-[ ] Create a more "real-life" code sample.
328
+
- Initial release (not yet released).
337
329
338
330
# Found a bug or have a feature request?
339
331
340
332
Please open an issue and be as clear as possible; see _How to contribute?_ for more information.
341
333
342
334
# How to contribute?
343
335
344
-
If you would like to contribute to the project, first, thank you for your interest, and please read [Contributing to ForEvolve open source projects](https://github.com/ForEvolve/ForEvolve.DependencyInjection/tree/master/CONTRIBUTING.md) for more information.
336
+
If you would like to contribute to the project, first, thank you for your interest, and please read [Contributing to ForEvolve open source projects](https://github.com/ForEvolve/Toc/blob/master/CONTRIBUTING.md) for more information.
345
337
346
338
## Contributor Covenant Code of Conduct
347
339
348
-
Also, please read the [Contributor Covenant Code of Conduct](https://github.com/ForEvolve/ForEvolve.DependencyInjection/tree/master/CODE_OF_CONDUCT.md) that applies to all ForEvolve repositories.
340
+
Also, please read the [Contributor Covenant Code of Conduct](https://github.com/ForEvolve/Toc/blob/master/CODE_OF_CONDUCT.md) that applies to all ForEvolve repositories.
0 commit comments