Add WebAPIExceptionHandler

This commit is contained in:
kaso17
2017-01-13 17:00:02 +01:00
parent 331de4247b
commit 33aac7e602
3 changed files with 65 additions and 0 deletions

View File

@@ -345,6 +345,7 @@
<Compile Include="Utils\WebAPIRequestLogger.cs" />
<Compile Include="Utils\WebAPIToNLogTracer.cs" />
<Compile Include="Utils\Clients\HttpWebClient.cs" />
<Compile Include="WebAPIExceptionHandler.cs" />
<Compile Include="WebAPIExceptionLogger.cs" />
<Compile Include="Indexers\BakaBT.cs" />
</ItemGroup>

View File

@@ -16,6 +16,7 @@ using Jackett.Services;
using System.Web.Http.Tracing;
using Jackett.Utils;
using Microsoft.AspNet.Identity;
using System.Web.Http.ExceptionHandling;
[assembly: OwinStartup(typeof(Startup))]
namespace Jackett
@@ -77,6 +78,9 @@ namespace Jackett
appBuilder.Use<WebApiRootRedirectMiddleware>();
// register exception handler
config.Services.Replace(typeof(IExceptionHandler), new WebAPIExceptionHandler());
// Setup tracing if enabled
if (TracingEnabled)
{

View File

@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.ExceptionHandling;
using Jackett.Utils;
using System.Net.Http;
using System.Net;
using System.Web.Http;
using System.Net.Sockets;
namespace Jackett
{
class WebAPIExceptionHandler : IExceptionHandler
{
public virtual Task HandleAsync(ExceptionHandlerContext context,
CancellationToken cancellationToken)
{
if (!ShouldHandle(context))
{
return Task.FromResult(0);
}
return HandleAsyncCore(context, cancellationToken);
}
public virtual Task HandleAsyncCore(ExceptionHandlerContext context,
CancellationToken cancellationToken)
{
HandleCore(context);
return Task.FromResult(0);
}
public virtual void HandleCore(ExceptionHandlerContext context)
{
// attempt to fix #930
if (context.Exception is SocketException)
{
Engine.Logger.Error("Ignoring unhandled SocketException: " + context.Exception.GetExceptionDetails());
return;
}
Engine.Logger.Error("HandleCore(): unhandled exception: " + context.Exception.GetExceptionDetails());
var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(context.Exception.Message),
ReasonPhrase = "Jackett_InternalServerError"
};
throw new HttpResponseException(resp);
}
public virtual bool ShouldHandle(ExceptionHandlerContext context)
{
return context.ExceptionContext.CatchBlock.IsTopLevel;
}
}
}