You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.7 KiB
59 lines
1.7 KiB
using System;
|
|
using FluentValidation;
|
|
using NLog;
|
|
using Nancy;
|
|
using NzbDrone.Api.Extensions;
|
|
using NzbDrone.Common.Exceptions;
|
|
using HttpStatusCode = Nancy.HttpStatusCode;
|
|
|
|
namespace NzbDrone.Api.ErrorManagement
|
|
{
|
|
public class NzbDroneErrorPipeline
|
|
{
|
|
private readonly Logger _logger;
|
|
|
|
public NzbDroneErrorPipeline(Logger logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public Response HandleException(NancyContext context, Exception exception)
|
|
{
|
|
var apiException = exception as ApiException;
|
|
|
|
if (apiException != null)
|
|
{
|
|
_logger.WarnException("API Error", apiException);
|
|
return apiException.ToErrorResponse();
|
|
}
|
|
|
|
var validationException = exception as ValidationException;
|
|
|
|
if (validationException != null)
|
|
{
|
|
_logger.Warn("Invalid request {0}", validationException.Message);
|
|
|
|
return validationException.Errors.AsResponse(HttpStatusCode.BadRequest);
|
|
}
|
|
|
|
var clientException = exception as NzbDroneClientException;
|
|
|
|
if (clientException != null)
|
|
{
|
|
return new ErrorModel
|
|
{
|
|
Message = exception.Message,
|
|
Description = exception.ToString()
|
|
}.AsResponse((HttpStatusCode)clientException.StatusCode);
|
|
}
|
|
|
|
_logger.FatalException("Request Failed", exception);
|
|
|
|
return new ErrorModel
|
|
{
|
|
Message = exception.Message,
|
|
Description = exception.ToString()
|
|
}.AsResponse(HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
} |