示例#1
0
 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));
 }
示例#2
0
 public async Task Invoke(HttpContext context)
 {
     try
     {
         await _next(context);
     }
     catch (UnauthorizedAccessException)
     {
         var exModel = new GeneralError {
             Message = "Not Authenticated", Status = HttpStatusCode.Unauthorized
         };
         await ContextDecorator(context, exModel);
     }
     catch (AuthenticationException)
     {
         var exModel = new GeneralError {
             Message = "Not Authorized", Status = HttpStatusCode.Forbidden
         };
         await ContextDecorator(context, exModel);
     }
     catch (ValidationException)
     {
         var exModel = new GeneralError {
             Message = "ValidationException", Status = HttpStatusCode.BadRequest
         };
         await ContextDecorator(context, exModel);
     }
     catch (Exception ex)
     {
         var exModel = new GeneralError {
             Message = "An error occurred", Status = HttpStatusCode.InternalServerError
         };
         _logService.Error(ex.Message, ex);
         await ContextDecorator(context, exModel);
     }
 }