public override void OnException(HttpActionExecutedContext ctx) { HttpStatusCode responseStatus = HttpStatusCode.InternalServerError; HttpExceptionData responseData = new HttpExceptionData(); ExceptionTranslator.TranslateException(ctx.Exception, ref responseStatus, ref responseData); if (!ShowStackTrace) { responseData.ErrorStackTrace = null; } ctx.Response = ctx.Request.CreateResponse(responseStatus, responseData); }
public void TranslateException(Exception exception, ref HttpStatusCode responseStatus, ref HttpExceptionData responseData) { if (exception is AggregateException) { exception = ExceptionUtils.GetInnerException(exception); } responseData.Message = Strings.HttpExceptionDefaultMessage; responseData.SetFromException(exception); if (exception is IBusinessException) { responseStatus = HttpStatusCode.PreconditionFailed; responseData.Message = exception.Message; } else if (exception is JsonBindingException) { responseStatus = HttpStatusCode.BadRequest; responseData.Message = Strings.MalformedRequestData; responseData.SetFromException(exception.InnerException); } else { string message = null; Exception newException = null; foreach (var t in customTranslators) { if (t.TranslateException(exception, out message, out newException)) { responseData.Message = message; responseData.SetFromException(newException); exception = newException; break; } } } if (exception is IHttpStatusCodeException) { responseStatus = (exception as IHttpStatusCodeException).GetStatusCode(); } if (exception is IDataException) { responseData.ErrorData = (exception as IDataException).GetExceptionData(); } }