Skip to content

Commit

Permalink
Merge pull request #12 from ejball/handler-options
Browse files Browse the repository at this point in the history
  • Loading branch information
ejball authored Dec 12, 2023
2 parents c4fa4ec + a1e683c commit 5b690bb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionPrefix>3.7.0</VersionPrefix>
<VersionPrefix>3.8.0</VersionPrefix>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
4 changes: 4 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 3.8.0

* Add `FacilityExceptionHandlerOptions.ContentSerializer`.

## 3.7.0

* Update Facility and FacilityCSharp to add support for `datetime` fields.
Expand Down
19 changes: 16 additions & 3 deletions src/Facility.AspNetCore/FacilityAspNetCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,32 @@ public static IApplicationBuilder UseFacilityHttpHandler<T>(this IApplicationBui
/// Adds a Facility service exception handler to the pipeline.
/// </summary>
/// <remarks>Do not include error details in production.</remarks>
public static IApplicationBuilder UseFacilityExceptionHandler(this IApplicationBuilder builder, bool includeErrorDetails = false)
public static IApplicationBuilder UseFacilityExceptionHandler(this IApplicationBuilder builder, Action<FacilityExceptionHandlerOptions>? configure = null)
{
if (builder == null)
throw new ArgumentNullException(nameof(builder));

var options = new FacilityExceptionHandlerOptions();
configure?.Invoke(options);

var includeErrorDetails = options.IncludeErrorDetails;
var contentSerializer = options.ContentSerializer ?? HttpContentSerializer.Create(SystemTextJsonServiceSerializer.Instance);

return builder.UseExceptionHandler(
options => options.Run(async context =>
x => x.Run(async context =>
{
var exception = context.Features.Get<IExceptionHandlerFeature>()?.Error;
var error = includeErrorDetails && exception != null ? ServiceErrorUtility.CreateInternalErrorForException(exception) : ServiceErrors.CreateInternalError();
using var httpResponseMessage = FacilityAspNetCoreUtility.CreateHttpResponseMessage(error);
using var httpResponseMessage = FacilityAspNetCoreUtility.CreateHttpResponseMessage(error, contentSerializer);
await FacilityAspNetCoreUtility.WriteHttpResponseMessageAsync(httpResponseMessage, context.Response);
}));
}

/// <summary>
/// Adds a Facility service exception handler to the pipeline.
/// </summary>
/// <remarks>Do not include error details in production.</remarks>
public static IApplicationBuilder UseFacilityExceptionHandler(this IApplicationBuilder builder, bool includeErrorDetails) =>
builder.UseFacilityExceptionHandler(x => x.IncludeErrorDetails = includeErrorDetails);
}
}
17 changes: 11 additions & 6 deletions src/Facility.AspNetCore/FacilityAspNetCoreUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ public static HttpRequestMessage CreateHttpRequestMessage(HttpRequest httpReques
return httpRequestMessage;
}

[Obsolete("Specify contentSerializer.")]
public static HttpResponseMessage CreateHttpResponseMessage(Exception exception) =>
CreateHttpResponseMessage(ServiceErrorUtility.CreateInternalErrorForException(exception));
CreateHttpResponseMessage(exception, HttpContentSerializer.Create(SystemTextJsonServiceSerializer.Instance));

public static HttpResponseMessage CreateHttpResponseMessage(ServiceErrorDto error)
[Obsolete("Specify contentSerializer.")]
public static HttpResponseMessage CreateHttpResponseMessage(ServiceErrorDto error) =>
CreateHttpResponseMessage(error, HttpContentSerializer.Create(SystemTextJsonServiceSerializer.Instance));

public static HttpResponseMessage CreateHttpResponseMessage(Exception exception, HttpContentSerializer contentSerializer) =>
CreateHttpResponseMessage(ServiceErrorUtility.CreateInternalErrorForException(exception), contentSerializer);

public static HttpResponseMessage CreateHttpResponseMessage(ServiceErrorDto error, HttpContentSerializer contentSerializer)
{
var statusCode = HttpServiceErrors.TryGetHttpStatusCode(error.Code) ?? HttpStatusCode.InternalServerError;
return new HttpResponseMessage(statusCode)
{
Content = HttpContentSerializer.Create(SystemTextJsonServiceSerializer.Instance).CreateHttpContent(error),
};
return new HttpResponseMessage(statusCode) { Content = contentSerializer.CreateHttpContent(error) };
}

public static async Task WriteHttpResponseMessageAsync(HttpResponseMessage httpResponseMessage, HttpResponse contextResponse)
Expand Down
10 changes: 10 additions & 0 deletions src/Facility.AspNetCore/FacilityExceptionHandlerOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Facility.Core.Http;

namespace Facility.AspNetCore;

public sealed class FacilityExceptionHandlerOptions
{
public bool IncludeErrorDetails { get; set; }

public HttpContentSerializer? ContentSerializer { get; set; }
}

0 comments on commit 5b690bb

Please sign in to comment.