1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Api/ErrorManagement/NzbDroneErrorPipeline.cs
2013-05-10 20:43:52 -07:00

49 lines
1.3 KiB
C#

using System;
using FluentValidation;
using NLog;
using Nancy;
using NzbDrone.Api.Extensions;
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);
}
_logger.FatalException("Request Failed", exception);
return new ErrorModel()
{
Message = exception.Message,
Description = exception.ToString()
}.AsResponse(HttpStatusCode.InternalServerError);
}
}
}