private async Task HandleExceptionAsync(HttpContext context, Exception exception) { var exceptionObject = new ExceptionResponse(exception); var exceptionSerialized = JsonConvert.SerializeObject(exceptionObject); if (!context.Response.HasStarted) { context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)exceptionObject.Error.Code; await context.Response.WriteAsync(exceptionSerialized); } }
private ExceptionResponse GetCompleteException(Exception exception) { var newException = exception is BusinessException ? (BusinessException)exception : null; var httpStatusCode = newException == null ? HttpStatusCode.InternalServerError : newException.httpStatusCode; var response = new ExceptionResponse { Error = new ErrorObject { Message = exception.Message, Target = exception.Source, Code = httpStatusCode }, InnerError = exception.InnerException != null ? GetCompleteException(exception.InnerException) : default }; return(response); }