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

Commit c188820

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
response headers handler
1 parent 45d3c4f commit c188820

File tree

6 files changed

+54
-31
lines changed

6 files changed

+54
-31
lines changed

Commmunity.AspNetCore.ExceptionHandling.Integration/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void ConfigureServices(IServiceCollection services)
2828
services.AddExceptionHandlingPolicies(options =>
2929
{
3030
options.For<ArgumentOutOfRangeException>().AddLog().AddRethrow();
31-
options.For<InvalidCastException>().AddLog().AddStatusCode(e => 400).AddHeaders((h, e) => h["X-qwe"] = e.Message);
31+
options.For<InvalidCastException>().AddLog().AddNewResponse(e => 400).WithHeaders((h, e) => h["X-qwe"] = e.Message);
3232
});
3333

3434
services.AddLogging(b => b.AddConsole());
@@ -37,7 +37,7 @@ public void ConfigureServices(IServiceCollection services)
3737
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
3838
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
3939
{
40-
app.UseDeveloperExceptionPage().UseExceptionHandlingPolicies();
40+
app.UseDeveloperExceptionPage().UseExceptionHandler().UseExceptionHandlingPolicies();
4141
app.UseMvc();
4242
}
4343
}

Commmunity.AspNetCore.ExceptionHandling/Builder/ExceptionMapping.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44
using Microsoft.Extensions.DependencyInjection;
55

6-
public class ExceptionMapping<TException> : IExceptionPolicyBuilder
6+
public class ExceptionMapping<TException> : IExceptionPolicyBuilder, IExceptionMapping<TException>, IResponseHandlers<TException>
77
where TException : Exception
88
{
99
public IExceptionPolicyBuilder Builder { get; }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace Commmunity.AspNetCore.ExceptionHandling.Builder
4+
{
5+
public interface IExceptionMapping<TException> : IExceptionPolicyBuilder
6+
where TException : Exception
7+
{
8+
}
9+
10+
public interface IResponseHandlers<TException> : IExceptionPolicyBuilder
11+
where TException : Exception
12+
{
13+
}
14+
}

Commmunity.AspNetCore.ExceptionHandling/PolicyBuilderExtensions.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,34 @@ namespace Commmunity.AspNetCore.ExceptionHandling
1111
{
1212
public static class PolicyBuilderExtensions
1313
{
14-
public static ExceptionMapping<TException> For<TException>(
14+
public static IExceptionMapping<TException> For<TException>(
1515
this IExceptionPolicyBuilder builder, int index = -1) where TException : Exception
1616
{
1717
builder.Options.EnsureException(typeof(TException), index);
1818
return new ExceptionMapping<TException>(builder);
1919
}
2020

21-
public static ExceptionMapping<TException> EnsureHandler<TException,THandler>(
22-
this ExceptionMapping<TException> builder, int index = -1)
21+
public static void EnsureHandler<TException,THandler>(
22+
this IExceptionPolicyBuilder builder, int index = -1)
2323
where THandler : class , IExceptionHandler
2424
where TException : Exception
2525

2626
{
2727
builder.Options.Value.EnsureHandler(typeof(TException), typeof(THandler), index);
2828
builder.Services.TryAddSingleton<THandler>();
29-
return builder;
3029
}
3130

32-
public static ExceptionMapping<TException> RemoveHandler<TException, THandler>(
33-
this ExceptionMapping<TException> builder)
31+
public static IExceptionMapping<TException> RemoveHandler<TException, THandler>(
32+
this IExceptionMapping<TException> builder)
3433
where THandler : IExceptionHandler
3534
where TException : Exception
3635
{
3736
builder.Options.Value.RemoveHandler(typeof(TException), typeof(THandler));
3837
return builder;
3938
}
4039

41-
public static ExceptionMapping<TException> Clear<TException>(
42-
this ExceptionMapping<TException> builder)
40+
public static IExceptionMapping<TException> Clear<TException>(
41+
this IExceptionMapping<TException> builder)
4342
where TException : Exception
4443
{
4544
builder.Options.Value.ClearHandlers(typeof(TException));
@@ -48,39 +47,44 @@ public static ExceptionMapping<TException> Clear<TException>(
4847

4948
// rethrow
5049
public static IExceptionPolicyBuilder AddRethrow<TException>(
51-
this ExceptionMapping<TException> builder, int index = -1)
50+
this IExceptionMapping<TException> builder, int index = -1)
5251
where TException : Exception
53-
{
54-
return builder.EnsureHandler<TException,ReThrowExceptionHandler>(index);
52+
{
53+
builder.EnsureHandler<TException, ReThrowExceptionHandler>(index);
54+
return builder;
5555
}
5656

5757
// Log
58-
public static ExceptionMapping<TException> AddLog<TException>(
59-
this ExceptionMapping<TException> builder, Action<LogHandlerOptions<TException>> settings = null, int index = -1)
58+
public static IExceptionMapping<TException> AddLog<TException>(
59+
this IExceptionMapping<TException> builder, Action<LogHandlerOptions<TException>> settings = null, int index = -1)
6060
where TException : Exception
6161
{
6262
LogHandlerOptions<TException> options = new LogHandlerOptions<TException>();
6363
settings?.Invoke(options);
6464
builder.Services.TryAddSingleton(options);
65-
return builder.EnsureHandler<TException, LogExceptionHandler<TException>>(index);
65+
builder.EnsureHandler<TException, LogExceptionHandler<TException>>(index);
66+
67+
return builder;
6668
}
6769

6870
// Set status code
69-
public static ExceptionMapping<TException> AddStatusCode<TException>(
70-
this ExceptionMapping<TException> builder, Func<TException,int> settings = null, int index = -1)
71+
public static IResponseHandlers<TException> AddNewResponse<TException>(
72+
this IExceptionMapping<TException> builder, Func<TException,int> statusCodeFactory = null, int index = -1)
7173
where TException : Exception
7274
{
73-
if (settings != null)
75+
if (statusCodeFactory != null)
7476
{
75-
builder.Services.Configure<SetStatusCodeOptions<TException>>(codeOptions =>
76-
codeOptions.StatusCodeFactory = settings);
77+
builder.Services.Configure<NewResponseOptions<TException>>(codeOptions =>
78+
codeOptions.StatusCodeFactory = statusCodeFactory);
7779
}
78-
79-
return builder.EnsureHandler<TException, SetStatusCodeHandler<TException>>(index);
80+
81+
builder.EnsureHandler<TException, NewResponseHandler<TException>>(index);
82+
83+
return builder as IResponseHandlers<TException>;
8084
}
8185

82-
public static ExceptionMapping<TException> AddHeaders<TException>(
83-
this ExceptionMapping<TException> builder, Action<IHeaderDictionary,TException> settings = null, int index = -1)
86+
public static IResponseHandlers<TException> WithHeaders<TException>(
87+
this IResponseHandlers<TException> builder, Action<IHeaderDictionary,TException> settings = null, int index = -1)
8488
where TException : Exception
8589
{
8690
if (settings != null)
@@ -89,7 +93,9 @@ public static ExceptionMapping<TException> AddHeaders<TException>(
8993
codeOptions.SetHeadersAction = settings);
9094
}
9195

92-
return builder.EnsureHandler<TException, SetHeadersHandler<TException>>(index);
96+
builder.EnsureHandler<TException, SetHeadersHandler<TException>>(index);
97+
98+
return builder;
9399
}
94100
}
95101
}

Commmunity.AspNetCore.ExceptionHandling/Response/SetStatusCodeHandler.cs renamed to Commmunity.AspNetCore.ExceptionHandling/Response/NewResponseHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Commmunity.AspNetCore.ExceptionHandling.Response
66
{
7-
public class SetStatusCodeHandler<TException> : RawResponseExceptionHandler<TException>
7+
public class NewResponseHandler<TException> : RawResponseExceptionHandler<TException>
88
where TException : Exception
99
{
10-
public SetStatusCodeHandler(IOptions<SetStatusCodeOptions<TException>> options,
10+
public NewResponseHandler(IOptions<NewResponseOptions<TException>> options,
1111
ILoggerFactory loggerFactory) : base(options.Value, loggerFactory)
1212
{
1313
}

Commmunity.AspNetCore.ExceptionHandling/Response/SetStatusCodeOptions.cs renamed to Commmunity.AspNetCore.ExceptionHandling/Response/NewResponseOptions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Commmunity.AspNetCore.ExceptionHandling.Response
66
{
7-
public class SetStatusCodeOptions<TException> : IOptions<SetStatusCodeOptions<TException>>, IOptions<RawResponseHandlerOptions<TException>>
7+
public class NewResponseOptions<TException> : IOptions<NewResponseOptions<TException>>, IOptions<RawResponseHandlerOptions<TException>>
88
where TException : Exception
99
{
10-
public SetStatusCodeOptions<TException> Value => this;
10+
public NewResponseOptions<TException> Value => this;
1111

1212
public Func<TException, int> StatusCodeFactory =
1313
exception => StatusCodes.Status500InternalServerError;
@@ -18,6 +18,9 @@ public class SetStatusCodeOptions<TException> : IOptions<SetStatusCodeOptions<TE
1818
SetResponse =
1919
async (context, exception) =>
2020
{
21+
context.Response.Headers.Clear();
22+
if (context.Response.Body.CanSeek)
23+
context.Response.Body.SetLength(0L);
2124
context.Response.StatusCode = this.StatusCodeFactory(exception);
2225
}
2326
};

0 commit comments

Comments
 (0)