public static int ToStatusCode(this Exception ex)
{
var hasStatusCode = ex as IHasStatusCode;
if (hasStatusCode != null)
return hasStatusCode.StatusCode;
if (HostContext.AppHost != null && HostContext.Config != null)
{
var exType = ex.GetType();
foreach (var entry in HostContext.Config.MapExceptionToStatusCode)
{
if (entry.Key.IsAssignableFromType(exType))
return entry.Value;
}
}
if (ex is HttpError) return ((HttpError)ex).Status;
if (ex is NotImplementedException || ex is NotSupportedException) return (int)HttpStatusCode.MethodNotAllowed;
if (ex is ArgumentException || ex is SerializationException || ex is FormatException) return (int)HttpStatusCode.BadRequest;
if (ex is AuthenticationException) return (int)HttpStatusCode.Unauthorized;
if (ex is UnauthorizedAccessException) return (int)HttpStatusCode.Forbidden;
if (ex is OptimisticConcurrencyException) return (int)HttpStatusCode.Conflict;
return (int)HttpStatusCode.InternalServerError;
}