diff --git a/src/Jackett/Jackett.csproj b/src/Jackett/Jackett.csproj
index 9c7db8842..dcc56078b 100644
--- a/src/Jackett/Jackett.csproj
+++ b/src/Jackett/Jackett.csproj
@@ -345,6 +345,7 @@
+
diff --git a/src/Jackett/Startup.cs b/src/Jackett/Startup.cs
index 52e24dbbc..cf40d8ee9 100644
--- a/src/Jackett/Startup.cs
+++ b/src/Jackett/Startup.cs
@@ -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();
+ // register exception handler
+ config.Services.Replace(typeof(IExceptionHandler), new WebAPIExceptionHandler());
+
// Setup tracing if enabled
if (TracingEnabled)
{
diff --git a/src/Jackett/WebAPIExceptionHandler.cs b/src/Jackett/WebAPIExceptionHandler.cs
new file mode 100644
index 000000000..74c7d085b
--- /dev/null
+++ b/src/Jackett/WebAPIExceptionHandler.cs
@@ -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;
+ }
+ }
+}