Handle argument exceptions on client side

This commit is contained in:
Maksym Pavlenko
2016-12-18 15:32:50 -08:00
parent 3ee90722d0
commit ddc1cdf4b3
5 changed files with 36 additions and 6 deletions
@@ -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;
@@ -16,6 +16,7 @@ using Podsync.Services.Storage;
namespace Podsync.Controllers
{
[Route("feed")]
[HandleException]
public class FeedController : Controller
{
private const int DefaultPageSize = 50;
@@ -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);
}
}
}
}
+1 -1
View File
@@ -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
+9 -5
View File
@@ -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 {