Skip to content
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

Add FacilityExceptionHandlerOptions.ContentSerializer. #12

Merged
merged 2 commits into from
Dec 12, 2023
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
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; }
}