private async Task ContextDecorator(HttpContext context, GeneralError error) { context.Response.Clear(); context.Response.ContentType = "json"; context.Response.StatusCode = error.Status.GetHashCode(); await context.Response.WriteAsync(JsonConvert.SerializeObject(error)); }
public async Task Invoke(HttpContext context) { try { await _next(context); } catch (AuthenticationException ex) { var exModel = new GeneralError { Message = "Not Authorized", Status = HttpStatusCode.Unauthorized }; await ContextDecorator(context, exModel); } catch (Exception ex) { //Log at var exModel = new GeneralError { Message = _hostingEnvironment.IsDevelopment() ? ex.ToString() : "An error occurred", Status = HttpStatusCode.InternalServerError }; await ContextDecorator(context, exModel); } }