public override void OnException(ExceptionContext context) { CoreError dgmError; switch (context.Exception) { case CoreException exception: var ex = exception; context.Exception = null; dgmError = ex.Error; break; case UnauthorizedAccessException _: dgmError = new CoreError("Unauthorized access", StatusCodes.Status401Unauthorized); break; default: var env = (IHostingEnvironment)context.HttpContext.RequestServices.GetService(typeof(IHostingEnvironment)); var msg = "An unhandled error occurred."; string stack = null; if (!env.IsProduction()) { msg = context.Exception.Message; stack = context.Exception.StackTrace; } if (context.Exception is InvalidDataException invalidDataException) { if (invalidDataException.Message.StartsWith("Multipart body length limit")) { msg = string.Format(_localizer["MultipartBodyLengthLimit_Message"], DefineValueList.FormOptionsMultipartBodyLengthLimit / 1048576); stack = null; } } if (context.Exception is Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException badHttpRequestException) { if (badHttpRequestException.Message.StartsWith("Request body too large")) { msg = string.Format(_localizer["MultipartBodyLengthLimit_Message"], DefineValueList.FormOptionsMultipartBodyLengthLimit / 1048576); stack = null; } } dgmError = new CoreError($"{msg} {stack}".Trim()); break; } if (context.HttpContext.Request.Path.Value.StartsWith("/api", StringComparison.OrdinalIgnoreCase)) { context.HttpContext.Response.StatusCode = dgmError.StatusCode; context.Result = new JsonResult(dgmError, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); } base.OnException(context); }
public CoreException(CoreValidationError validationError) { Error = new CoreError(new List <CoreValidationError> { validationError }); }
public CoreException(IEnumerable <CoreValidationError> validationErrors) { Error = new CoreError(validationErrors); }
public CoreException(string message) { Error = new CoreError(new List <CoreValidationError> { new CoreValidationError(message) }); }