Как я могу поймать ASP. NET Core (3.1) ошибку разбора URL с моим промежуточным программным обеспечением на основе пользовательских исключений? - PullRequest
1 голос
/ 17 апреля 2020

У меня есть промежуточное программное обеспечение с пользовательскими исключениями, добавленное в проект ASP. NET Core 3.1 (WebAPI). Промежуточное ПО довольно простое, оно перехватывает определенные c типы исключений и передает соответствующее сообщение потребителю, в то время как оно отправляет универсальное c сообщение об ошибке для всего остального.

Вот класс промежуточного программного обеспечения:

    /// <summary>
    /// Middleware which catches controller exceptions and extracts exceptions which are specifically intended for the 
    /// client and sends them as a regular response (and not 500 server error).
    /// </summary>
    public class ExceptionMiddleware
    {
        private readonly RequestDelegate _next;
        private ILogger<ExceptionMiddleware> _logger;

        public ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> logger)
        {
            _next = next;
            _logger = logger;
        }

        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (MDEException ex) // we only care about this particular exception
            {
                // Send exception message as plain message
                _logger.Log(LogLevel.Error, ex.Message);
                context.Response.ContentType = "text/plain";
                context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                await context.Response.WriteAsync(ex.Message);
            }
            catch (Exception ex)
            {
                // Send generic error as plain message
                _logger.Log(LogLevel.Error, ex, ex.Message);
                context.Response.ContentType = "text/plain";
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                await context.Response.WriteAsync("Es trat ein unvorhergesehenes Problem auf - bitte kontaktieren Sie Ihren Administrator!");
            }
        }
    }

Все это прекрасно работает, за исключением случаев, когда я отправляю неверный URL-адрес приложению. В этом случае мое промежуточное ПО даже не трогается. Например, если я использую этот URL: api/vm/ziel-standort/stellplatzId=2 Я получаю сообщение об ошибке, похожее на это:

{"errors":{"stellplatzId":["The value 'stellplatzId=2' is not valid."]},"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|d7456c7b-469208468c4ac571."}

Вот как выглядит мой метод Configure:

/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app">Application builder (injected by framework)</param>
/// <param name="log">Logger (injected by framework)</param>
/// <param name="actionProvider">Action provider for listing routes (injected by framework)</param>
public void Configure(IApplicationBuilder app, ILogger<Startup> log, Microsoft.AspNetCore.Mvc.Infrastructure.IActionDescriptorCollectionProvider actionProvider)
{
    log.Log(LogLevel.Information, "Configuring application ...");

    log.Log(LogLevel.Debug, "Using CorsPolicy");
    app.UseCors("CorsPolicy");

    log.Log(LogLevel.Debug, "Using Exception middleware (MDEException message pass-through to client)");
    app.UseMiddleware<ExceptionMiddleware>();

    log.Log(LogLevel.Debug, "Enabling routing");
    app.UseRouting();

    log.Log(LogLevel.Debug, "Enabling authentication and authorization");
    app.UseAuthentication();
    app.UseAuthorization();

    log.Log(LogLevel.Debug, "Setting up routing for controllers");
    app.UseEndpoints(opt =>
    {
        opt.MapControllers();
    });

    // .....
}

Как мне сделать мое промежуточное ПО ловит эту ошибку?

1 Ответ

1 голос
/ 17 апреля 2020

Вы получаете ошибку проверки модели, а не исключение. Чтобы переопределить это, вы можете использовать приведенный ниже код в методе ConfigureServices класса Startup Класс:

        services.AddControllers()
            .ConfigureApiBehaviorOptions(o =>
            {
                o.InvalidModelStateResponseFactory = context =>
                {
                    var _logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<Startup>>();
                    var ex = new Exception(context.ModelState.Values.First().Errors.First().ErrorMessage);
                    _logger.Log(LogLevel.Error, ex, ex.Message);
                    context.HttpContext.Response.ContentType = "text/plain";
                    context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    context.HttpContext.Response.WriteAsync("Es trat ein unvorhergesehenes Problem auf - bitte kontaktieren Sie Ihren Administrator!");
                    return new EmptyResult();
                };
            });

И вот как обработка исключений должна выполняться на основе документов Microsoft:

        app.UseExceptionHandler(errorApp =>
        {
            errorApp.Run(async context =>
            {
                var ex = context.Features.Get<IExceptionHandlerPathFeature>()?.Error;                    
                if (ex is MDEException) // we only care about this particular exception
                {
                    // Send exception message as plain message
                    _logger.Log(LogLevel.Error, ex.Message);
                    context.Response.ContentType = "text/plain";
                    context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    await context.Response.WriteAsync(ex.Message);
                }
                else
                {
                    // Send generic error as plain message
                    _logger.Log(LogLevel.Error, ex, ex.Message);
                    context.Response.ContentType = "text/plain";
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    await context.Response.WriteAsync("Es trat ein unvorhergesehenes Problem auf - bitte kontaktieren Sie Ihren Administrator!");
                }
            });
        });
...