Beispiel #1
0
        /// <summary>
        /// Fluxo que controla as exceptions geradas na aplicação
        /// </summary>
        /// <param name="context"></param>
        /// <param name="exception"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private Task HandleExceptionAsync(HttpContext context, Exception exception, ApiExceptionOptions options)
        {
            var statusCode = GetHttpStatusCodeByException(exception);

            var problemDetail = new ProblemDetail()
            {
                Id         = Guid.NewGuid().ToString(),
                CodigoHttp = statusCode,
                Titulo     = "Erro ocorrido na Api"
            };

            options.AddResponseDetails?.Invoke(context, exception, problemDetail);

            var innerException = GetInnerExceptionMessage(exception);

            var level = options.DetermineLogLevel?.Invoke(exception) ?? LogLevel.Error;

            _logger.Log(level, exception, $"BAD ERROR!! {innerException} -- {problemDetail.Id}");

            var result = JsonSerializer.Serialize(problemDetail);

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = statusCode;
            return(context.Response.WriteAsync(result));
        }
        public static IApplicationBuilder UseApiExceptionHandler(this IApplicationBuilder builder,
            Action<ApiExceptionOptions> configureOptions)
        {
            var options = new ApiExceptionOptions();
            configureOptions(options);

            return builder.UseMiddleware<ApiExceptionMiddleware>(options);
        }
Beispiel #3
0
 public ApiExceptionMiddleware(RequestDelegate next,
                               ILogger <ApiExceptionMiddleware> logger,
                               ApiExceptionOptions options,
                               IScopeInformation scopeInformation)
 {
     _next             = next;
     _logger           = logger;
     _options          = options;
     _scopeInformation = scopeInformation;
 }