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(); } }
public bool TranslateException(Exception exception, out string message, out Exception newException) { message = exception.Message; newException = exception; if (exception is ProviderIncompatibleException) { message = Strings.DbConnectionError; return(true); } if (exception is EntityCommandExecutionException) { message = Strings.DbCommandExecutionError; return(true); } if (exception is DbEntityValidationException) { ValidationRuleViolations violations = new ValidationRuleViolations(); violations.Add((exception as DbEntityValidationException).EntityValidationErrors); message = Strings.DbValidationError; newException = new DbWrappedException(message, exception, violations); return(true); } if (exception is DbUpdateException || exception is SqlException) { message = Strings.DbUpdateError; newException = ExceptionUtils.GetInnerException(exception); foreach (var regex in Strings.ConstraintRegexes) { if (regex.IsMatch(newException.Message)) { string constraintName = regex.Matches(newException.Message)[0].Groups["cName"].ToString(); string constraintMessage = GetConstraintMessage(constraintName); if (constraintMessage != null) { message = constraintMessage; } } } return(true); } return(false); }