コード例 #1
0
        public static void ConfigureExceptionHandler(this IApplicationBuilder app)
        {
            app.UseExceptionHandler(error =>
            {
                error.Run(async context =>
                {
                    var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                    var exception = exceptionHandlerFeature.Error;

                    var statusCode = exception switch
                    {
                        ResourceNotFoundException _ => (int)HttpStatusCode.NotFound,
                        ModelFormatException _ => (int)HttpStatusCode.PreconditionFailed,
                        ResourceAlreadyExistsException _ => (int)HttpStatusCode.Conflict,
                        InvalidLoginException _ => (int)HttpStatusCode.Unauthorized,
                        UnauthorizedException _ => (int)HttpStatusCode.Unauthorized,
                        InvalidProductIdentifierException _ => (int)HttpStatusCode.PreconditionFailed,
                        _ => (int)HttpStatusCode.InternalServerError
                    };

                    context.Response.ContentType = "application/json";
                    context.Response.StatusCode  = statusCode;

                    await context.Response.WriteAsync(new ExceptionModel
                    {
                        StatusCode = statusCode,
                        Message    = exception.Message
                    }.ToString());
                });
            });
        }
コード例 #2
0
        public static void UseGlobalExceptionHandler(this IApplicationBuilder app)
        {
            app.UseExceptionHandler(error =>
            {
                error.Run(async context =>
                {
                    var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>();
                    var exception = exceptionHandlerFeature.Error;
                    var log       = app.ApplicationServices.GetService(typeof(ILogService)) as ILogService;

                    var statusCode = exception switch
                    {
                        ResourceNotFoundException _ => (int)HttpStatusCode.NotFound,
                        ModelFormatException _ => (int)HttpStatusCode.PreconditionFailed,
                        ArgumentOutOfRangeException _ => (int)HttpStatusCode.BadRequest,
                        _ => (int)HttpStatusCode.InternalServerError
                    };

                    context.Response.ContentType = "application/json";
                    context.Response.StatusCode  = statusCode;

                    log.LogToDatabase(new ExceptionModel
                    {
                        StatusCode       = statusCode,
                        ExceptionMessage = exception.Message,
                        StackTrace       = exception.StackTrace
                    });
                    await context.Response.WriteAsync(new ExceptionModel
                    {
                        StatusCode       = statusCode,
                        ExceptionMessage = exception.Message,
                    }.ToString());
                });
            });
        }