2013-01-19 07:46:43 +03:00
|
|
|
using System;
|
2013-04-21 01:14:41 +03:00
|
|
|
using FluentValidation;
|
2013-01-19 07:46:43 +03:00
|
|
|
using NLog;
|
|
|
|
using Nancy;
|
2013-02-23 23:35:26 +03:00
|
|
|
using NzbDrone.Api.Extensions;
|
2013-08-02 10:16:37 +03:00
|
|
|
using HttpStatusCode = Nancy.HttpStatusCode;
|
2013-01-19 07:46:43 +03:00
|
|
|
|
2013-02-19 04:13:42 +03:00
|
|
|
namespace NzbDrone.Api.ErrorManagement
|
2013-01-19 07:46:43 +03:00
|
|
|
{
|
2013-05-11 02:53:50 +03:00
|
|
|
public class NzbDroneErrorPipeline
|
2013-01-19 07:46:43 +03:00
|
|
|
{
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-05-11 02:53:50 +03:00
|
|
|
public NzbDroneErrorPipeline(Logger logger)
|
2013-01-19 07:46:43 +03:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Response HandleException(NancyContext context, Exception exception)
|
|
|
|
{
|
2013-01-20 03:19:27 +03:00
|
|
|
var apiException = exception as ApiException;
|
2013-01-28 21:06:54 +03:00
|
|
|
|
2013-01-20 03:19:27 +03:00
|
|
|
if (apiException != null)
|
2013-01-19 07:46:43 +03:00
|
|
|
{
|
2013-01-20 03:19:27 +03:00
|
|
|
_logger.WarnException("API Error", apiException);
|
|
|
|
return apiException.ToErrorResponse();
|
2013-01-19 07:46:43 +03:00
|
|
|
}
|
2013-01-28 21:06:54 +03:00
|
|
|
|
2013-04-21 01:14:41 +03:00
|
|
|
var validationException = exception as ValidationException;
|
|
|
|
|
|
|
|
if (validationException != null)
|
|
|
|
{
|
|
|
|
_logger.Warn("Invalid request {0}", validationException.Message);
|
2013-04-29 03:39:17 +03:00
|
|
|
|
2013-04-21 01:14:41 +03:00
|
|
|
return validationException.Errors.AsResponse(HttpStatusCode.BadRequest);
|
|
|
|
}
|
|
|
|
|
2013-05-04 05:15:46 +03:00
|
|
|
_logger.FatalException("Request Failed", exception);
|
2013-01-28 21:06:54 +03:00
|
|
|
|
|
|
|
return new ErrorModel()
|
|
|
|
{
|
2013-04-29 03:39:17 +03:00
|
|
|
Message = exception.Message,
|
|
|
|
Description = exception.ToString()
|
2013-01-28 21:06:54 +03:00
|
|
|
}.AsResponse(HttpStatusCode.InternalServerError);
|
2013-01-19 07:46:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|