diff --git a/src/Podsync/Controllers/DownloadController.cs b/src/Podsync/Controllers/DownloadController.cs index e09cf4c..028e9f7 100644 --- a/src/Podsync/Controllers/DownloadController.cs +++ b/src/Podsync/Controllers/DownloadController.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Podsync.Helpers; using Podsync.Services.Links; using Podsync.Services.Resolver; using Podsync.Services.Storage; @@ -7,6 +8,7 @@ using Podsync.Services.Storage; namespace Podsync.Controllers { [Route("download")] + [HandleException] public class DownloadController : Controller { private readonly IResolverService _resolverService; diff --git a/src/Podsync/Controllers/FeedController.cs b/src/Podsync/Controllers/FeedController.cs index 73ed962..51a7691 100644 --- a/src/Podsync/Controllers/FeedController.cs +++ b/src/Podsync/Controllers/FeedController.cs @@ -16,6 +16,7 @@ using Podsync.Services.Storage; namespace Podsync.Controllers { [Route("feed")] + [HandleException] public class FeedController : Controller { private const int DefaultPageSize = 50; diff --git a/src/Podsync/Helpers/HandleExceptionAttribute.cs b/src/Podsync/Helpers/HandleExceptionAttribute.cs new file mode 100644 index 0000000..ce83200 --- /dev/null +++ b/src/Podsync/Helpers/HandleExceptionAttribute.cs @@ -0,0 +1,23 @@ +using System; +using System.Net; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace Podsync.Helpers +{ + public class HandleExceptionAttribute : ExceptionFilterAttribute + { + public override void OnException(ExceptionContext context) + { + var exception = context.Exception; + if (exception is ArgumentNullException || exception is ArgumentException) + { + context.Result = new BadRequestObjectResult(exception.Message); + } + else + { + context.Result = new StatusCodeResult((int)HttpStatusCode.InternalServerError); + } + } + } +} \ No newline at end of file diff --git a/src/Podsync/Services/Links/LinkService.cs b/src/Podsync/Services/Links/LinkService.cs index 742516b..7c813a7 100644 --- a/src/Podsync/Services/Links/LinkService.cs +++ b/src/Podsync/Services/Links/LinkService.cs @@ -173,7 +173,7 @@ namespace Podsync.Services.Links if (string.IsNullOrWhiteSpace(id) || linkType == LinkType.Unknown || provider == Provider.Unknown) { - throw new ArgumentException("Failed to parse URL"); + throw new ArgumentException("This provider is not supported"); } return new LinkInfo diff --git a/src/Podsync/wwwroot/js/site.js b/src/Podsync/wwwroot/js/site.js index 66951f9..c04e758 100644 --- a/src/Podsync/wwwroot/js/site.js +++ b/src/Podsync/wwwroot/js/site.js @@ -24,12 +24,16 @@ $(function () { error: function (xhr, status, error) { if (xhr.status === 400) { // Bad request - var json = JSON.parse(xhr.responseText); - var text = ''; - $.each(json, function (key, value) { - text += value + '\r\n'; - }); + + try { + var json = JSON.parse(xhr.responseText); + $.each(json, function (key, value) { + text += value + '\r\n'; + }); + } catch (e) { + text = xhr.responseText; + } err(text); } else {