Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 4b10406

Browse files
author
IharYakimush
authored
Update README.md
1 parent d713436 commit 4b10406

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,41 @@
11
# AspNetCore-ExceptionHandling
22
Middleware to configure exception handling policies. Configure chain of handlers per exception type. OOTB handlers: log, retry, set responce headers and body
3+
4+
### Sample
5+
```
6+
public void ConfigureServices(IServiceCollection services)
7+
{
8+
services.AddMvc();
9+
10+
services.AddExceptionHandlingPolicies(options =>
11+
{
12+
options.For<InitializationException>().Rethrow();
13+
14+
options.For<SomeTransientException>().Retry().NextChain();
15+
16+
options.For<SomeBadRequestException>()
17+
.Response(e => 400)
18+
.Headers((h, e) => h["X-MyCustomHeader"] = e.Message)
19+
.WithBody((req,sw, exception) => sw.WriteAsync(exception.ToString()))
20+
.NextChain();
21+
22+
// Ensure that all exception types are handled by adding handler for generic exception at the end.
23+
options.For<Exception>()
24+
.Log(lo =>
25+
{
26+
lo.EventIdFactory = (c, e) => new EventId(123, "UnhandlerException");
27+
lo.Category = (context, exception) => "MyCategory";
28+
})
29+
.Response(e => 500, ResponseAlreadyStartedBehaviour.GoToNextHandler)
30+
.ClearCacheHeaders()
31+
.WithObjectResult((r, e) => new { msg = e.Message, path = r.Path })
32+
.Handled();
33+
});
34+
}
35+
36+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
37+
{
38+
app.UseExceptionHandlingPolicies();
39+
app.UseMvc();
40+
}
41+
```

0 commit comments

Comments
 (0)