private static Task WriteCustomOutputAsync(ApiErrorOutput errorOutput, HttpContext context) { var stringResult = JsonConvert.SerializeObject(errorOutput, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); context.Response.ContentType = "application/json"; context.Response.StatusCode = errorOutput?.Status ?? 500; return(context.Response.WriteAsync(stringResult)); }
private static Task HandleExceptionAsync(DbUpdateException ex, HttpContext context, bool isDevelopment = false) { var errorOutput = new ApiErrorOutput { Title = "Database violation error.", Status = (int)HttpStatusCode.Conflict }; const string dbProductionMessage = "Some constraints were violated. Please contact system's administrators."; var messages = new[] { isDevelopment?ex.Message : dbProductionMessage }; errorOutput.AddErrorEntry("Database", messages); return(WriteCustomOutputAsync(errorOutput, context)); }
private static Task HandleExceptionAsync(Exception ex, HttpContext context, bool isDevelopment = false) { var errorOutput = new ApiErrorOutput { Title = "Unexpected Execution error.", Status = (int)HttpStatusCode.InternalServerError }; const string executionProductionMessage = "Sorry! A failure occurred during request. Please contact system's administrators."; var messages = isDevelopment ? ExtractExceptionMessages(ex).ToArray() : new[] { executionProductionMessage }; errorOutput.AddErrorEntry("Execution", messages); return(WriteCustomOutputAsync(errorOutput, context)); }