sonarr-repo-only/NzbDrone.Api/ErrorManagement/ErrorPipeline.cs

50 lines
1.3 KiB
C#
Raw Normal View History

using System;
using System.Linq;
2013-04-20 22:14:41 +00:00
using FluentValidation;
using NLog;
using Nancy;
2013-02-23 20:35:26 +00:00
using NzbDrone.Api.Extensions;
2013-02-19 01:13:42 +00:00
namespace NzbDrone.Api.ErrorManagement
{
public class ErrorPipeline
{
private readonly Logger _logger;
public ErrorPipeline(Logger logger)
{
_logger = logger;
}
public Response HandleException(NancyContext context, Exception exception)
{
var apiException = exception as ApiException;
2013-01-28 18:06:54 +00:00
if (apiException != null)
{
_logger.WarnException("API Error", apiException);
return apiException.ToErrorResponse();
}
2013-01-28 18:06:54 +00:00
2013-04-20 22:14:41 +00:00
var validationException = exception as ValidationException;
if (validationException != null)
{
_logger.Warn("Invalid request {0}", validationException.Message);
return validationException.Errors.AsResponse(HttpStatusCode.BadRequest);
}
_logger.ErrorException("Unexpected error", exception);
2013-01-28 18:06:54 +00:00
return new ErrorModel()
{
Message = exception.Message,
Description = exception.ToString()
}.AsResponse(HttpStatusCode.InternalServerError);
}
}
}