private void HandleException(HttpContext context, Exception ex) { string exceptionResponse; if (ex.GetType() == typeof(NotFoundException)) { exceptionResponse = ResponseCreator.CreateBadResponse((int)ErrorCode.NotFoundError, ex.Message); } else if (ex.GetType() == typeof(DBException)) { exceptionResponse = ResponseCreator.CreateBadResponse((int)ErrorCode.DBError, ex.Message); } else if (ex.GetType() == typeof(PermissionException)) { exceptionResponse = ResponseCreator.CreateBadResponse((int)ErrorCode.PermissionError, ex.Message); } else if (ex.GetType() == typeof(ValidationException)) { exceptionResponse = ResponseCreator.CreateBadResponse((int)ErrorCode.ValidationError, ex.Message); } else { exceptionResponse = ResponseCreator.CreateBadResponse((int)ErrorCode.ServerError, "server error"); } ErrorResposeWriter.WriteExceptionResponse(context, exceptionResponse); }
public async Task InvokeAsync(HttpContext context) { try { await _next(context); if (context.Response.StatusCode == 403) { ErrorResposeWriter.WriteExceptionResponse(context, "You don't have permission"); } } catch (Exception ex) { HandleException(context, ex); } }