Skip to content

Commit

Permalink
Add more XML docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejball committed Mar 25, 2024
1 parent 57cf4f1 commit 7317610
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Facility.AspNetCore/FacilityActionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@

namespace Facility.AspNetCore;

/// <summary>
/// Supports Facility types in controller actions (including code-generated controllers).
/// </summary>
public sealed class FacilityActionFilter : ActionFilterAttribute
{
/// <inheritdoc />
public override void OnActionExecuting(ActionExecutingContext context)
{
// replace empty httpRequest argument with the actual request
if (context.ActionArguments.TryGetValue(c_httpRequestKey, out var request) && request?.GetType() == typeof(HttpRequestMessage))
context.ActionArguments[c_httpRequestKey] = FacilityAspNetCoreUtility.CreateHttpRequestMessage(context.HttpContext.Request);
}

/// <inheritdoc />
public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
// special response handling for Facility types
Expand Down
15 changes: 15 additions & 0 deletions src/Facility.AspNetCore/FacilityAspNetCoreUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@

namespace Facility.AspNetCore;

/// <summary>
/// Utility methods for using Facility with ASP.NET Core.
/// </summary>
public sealed class FacilityAspNetCoreUtility
{
/// <summary>
/// Converts an <c>HttpRequest</c> to an <c>HttpRequestMessage</c>.
/// </summary>
public static HttpRequestMessage CreateHttpRequestMessage(HttpRequest httpRequest)
{
var encodedUrl = httpRequest.GetEncodedUrl();
Expand Down Expand Up @@ -36,15 +42,24 @@ public static HttpResponseMessage CreateHttpResponseMessage(Exception exception)
public static HttpResponseMessage CreateHttpResponseMessage(ServiceErrorDto error) =>
CreateHttpResponseMessage(error, HttpContentSerializer.Create(SystemTextJsonServiceSerializer.Instance));

/// <summary>
/// Creates an <c>HttpResponseMessage</c> for an exception.
/// </summary>
public static HttpResponseMessage CreateHttpResponseMessage(Exception exception, HttpContentSerializer contentSerializer) =>
CreateHttpResponseMessage(ServiceErrorUtility.CreateInternalErrorForException(exception), contentSerializer);

/// <summary>
/// Creates an <c>HttpResponseMessage</c> for an error.
/// </summary>
public static HttpResponseMessage CreateHttpResponseMessage(ServiceErrorDto error, HttpContentSerializer contentSerializer)
{
var statusCode = HttpServiceErrors.TryGetHttpStatusCode(error.Code) ?? HttpStatusCode.InternalServerError;
return new HttpResponseMessage(statusCode) { Content = contentSerializer.CreateHttpContent(error) };
}

/// <summary>
/// Writes an <c>HttpResponseMessage</c> to an <c>HttpResponse</c>.
/// </summary>
public static async Task WriteHttpResponseMessageAsync(HttpResponseMessage httpResponseMessage, HttpResponse contextResponse)
{
contextResponse.StatusCode = (int) httpResponseMessage.StatusCode;
Expand Down
4 changes: 4 additions & 0 deletions src/Facility.AspNetCore/FacilityEndpointFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

namespace Facility.AspNetCore;

/// <summary>
/// Supports Facility types returned from API routes (<c>ServiceResult</c> and <c>ServiceError</c>).
/// </summary>
public sealed class FacilityEndpointFilter : IEndpointFilter
{
/// <inheritdoc />
public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
var result = await next(context);
Expand Down
9 changes: 9 additions & 0 deletions src/Facility.AspNetCore/FacilityExceptionHandlerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

namespace Facility.AspNetCore;

/// <summary>
/// Options for <c>FacilityAspNetCoreExtensions.UseFacilityExceptionHandler</c>.
/// </summary>
public sealed class FacilityExceptionHandlerOptions
{
/// <summary>
/// True to include error details. Not for production.
/// </summary>
public bool IncludeErrorDetails { get; set; }

/// <summary>
/// The content serializer to use when writing an error to the response.
/// </summary>
public HttpContentSerializer? ContentSerializer { get; set; }
}

0 comments on commit 7317610

Please sign in to comment.