Example #1
0
        public static void ConfigureExceptionHandler(this IApplicationBuilder app)
        {
            app.UseExceptionHandler(appError =>
            {
                appError.Run(async context =>
                {
                    context.Response.StatusCode  = (int)HttpStatusCode.InternalServerError;
                    context.Response.ContentType = "application/json";

                    IExceptionHandlerFeature contextFeature = context.Features.Get <IExceptionHandlerFeature>();
                    if (contextFeature != null)
                    {
                        Exception error = contextFeature.Error;

                        string message = DefaultErrorMessage;

                        if (error is ArgumentException argumentException)
                        {
                            message = argumentException.Message;
                        }

                        var errorMsg = new ErrorModelInfo()
                        {
                            Message     = message,
                            ErrorObject = error.Message,
                            StatusCode  = context.Response.StatusCode,
                        };

                        await context.Response.WriteAsync(errorMsg.ToString());
                    }
                });
            });
        }
Example #2
0
        private ObjectResult SendError(string message, Exception ex = null, int status = 500)
        {
            ErrorModelInfo errorDetails = new ErrorModelInfo()
            {
                Message     = message,
                ErrorObject = ex?.Message,
                StatusCode  = status,
            };

            return(StatusCode(status, errorDetails));
        }