Add project files.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
// ***********************************************************************
|
||||
// <copyright file="HttpErrorMiddleware.cs">
|
||||
// Heath
|
||||
// </copyright>
|
||||
// ***********************************************************************
|
||||
|
||||
using Lib.Common.LoggingAPI.Common.Settings;
|
||||
using Lib.Common.LoggingAPI.Service.Constants;
|
||||
using Lib.Common.LoggingAPI.Service.DataTransferObjects.Error;
|
||||
using Lib.Common.LoggingAPI.Service.Middleware.HttpLogger;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Serilog;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Lib.Common.LoggingAPI.Service.Middleware.HttpException
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles HTTP logging.
|
||||
/// </summary>
|
||||
public class HttpErrorMiddleware
|
||||
{
|
||||
private readonly ILogger logger;
|
||||
private readonly RequestDelegate requestProcess;
|
||||
public readonly ServiceSettings settings;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instrance of <see cref="HttpErrorMiddleware"/>.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger representig an instance of <see cref="ILogger"/>.</param>
|
||||
/// <param name="requestProcess">The request delegate process.</param>
|
||||
public HttpErrorMiddleware(ILogger logger, RequestDelegate requestProcess, ServiceSettings settings)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.requestProcess = requestProcess;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invoke method.
|
||||
/// </summary>
|
||||
/// <param name="context">The HTTP context.</param>
|
||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await this.requestProcess(context).ConfigureAwait(false);
|
||||
}
|
||||
catch (Extensions.HttpException exception)
|
||||
{
|
||||
await HandleErrorResponse(
|
||||
context,
|
||||
exception.Message,
|
||||
exception.ErrorCode,
|
||||
exception.StatusCode).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception defaultException)
|
||||
{
|
||||
await HandleErrorResponse(
|
||||
context,
|
||||
defaultException.Message,
|
||||
ErrorCodes.InternalServerError,
|
||||
StatusCodes.Status500InternalServerError).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles error responses.
|
||||
/// </summary>
|
||||
/// <param name="context">The HTTP context.</param>
|
||||
/// <param name="message">The error message.</param>
|
||||
/// <param name="errorCode">The error code.</param>
|
||||
/// <param name="statusCode">The HTTP status code.</param>
|
||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||
private async Task HandleErrorResponse(HttpContext context, string? message, string? errorCode, int statusCode)
|
||||
{
|
||||
var errorMessage = new HttpErrorDto(
|
||||
message,
|
||||
errorCode,
|
||||
string.Format(
|
||||
Responses.Target,
|
||||
context.Request.Method,
|
||||
context.Request.Scheme,
|
||||
context.Request.Host.Host,
|
||||
context.Request.Path));
|
||||
|
||||
this.logger.LogError<HttpErrorDto>(context, errorMessage, settings.ServiceId);
|
||||
|
||||
context.Response.ContentType = MimeTypes.ApplicationJson;
|
||||
context.Response.StatusCode = statusCode;
|
||||
|
||||
await context.Response.WriteAsync(JsonSerializer.Serialize(errorMessage)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user