mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
Add WebAPIExceptionHandler
This commit is contained in:
@@ -345,6 +345,7 @@
|
|||||||
<Compile Include="Utils\WebAPIRequestLogger.cs" />
|
<Compile Include="Utils\WebAPIRequestLogger.cs" />
|
||||||
<Compile Include="Utils\WebAPIToNLogTracer.cs" />
|
<Compile Include="Utils\WebAPIToNLogTracer.cs" />
|
||||||
<Compile Include="Utils\Clients\HttpWebClient.cs" />
|
<Compile Include="Utils\Clients\HttpWebClient.cs" />
|
||||||
|
<Compile Include="WebAPIExceptionHandler.cs" />
|
||||||
<Compile Include="WebAPIExceptionLogger.cs" />
|
<Compile Include="WebAPIExceptionLogger.cs" />
|
||||||
<Compile Include="Indexers\BakaBT.cs" />
|
<Compile Include="Indexers\BakaBT.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@@ -16,6 +16,7 @@ using Jackett.Services;
|
|||||||
using System.Web.Http.Tracing;
|
using System.Web.Http.Tracing;
|
||||||
using Jackett.Utils;
|
using Jackett.Utils;
|
||||||
using Microsoft.AspNet.Identity;
|
using Microsoft.AspNet.Identity;
|
||||||
|
using System.Web.Http.ExceptionHandling;
|
||||||
|
|
||||||
[assembly: OwinStartup(typeof(Startup))]
|
[assembly: OwinStartup(typeof(Startup))]
|
||||||
namespace Jackett
|
namespace Jackett
|
||||||
@@ -77,6 +78,9 @@ namespace Jackett
|
|||||||
|
|
||||||
appBuilder.Use<WebApiRootRedirectMiddleware>();
|
appBuilder.Use<WebApiRootRedirectMiddleware>();
|
||||||
|
|
||||||
|
// register exception handler
|
||||||
|
config.Services.Replace(typeof(IExceptionHandler), new WebAPIExceptionHandler());
|
||||||
|
|
||||||
// Setup tracing if enabled
|
// Setup tracing if enabled
|
||||||
if (TracingEnabled)
|
if (TracingEnabled)
|
||||||
{
|
{
|
||||||
|
60
src/Jackett/WebAPIExceptionHandler.cs
Normal file
60
src/Jackett/WebAPIExceptionHandler.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user