added JsonErrorFilter to automatically handle failing ajax calls.

pull/4/head
kay.one 13 years ago
parent 7c6d745c86
commit b8ac694fc4

@ -9,6 +9,7 @@ using NzbDrone.Core.Jobs;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Web.Filters;
using NzbDrone.Web.Models;
namespace NzbDrone.Web.Controllers
@ -130,7 +131,7 @@ namespace NzbDrone.Web.Controllers
catch (Exception ex)
{
return Json(new NotificationResult() { Title = "Failed", Text = ex.Message, NotificationType = NotificationType.Error});
return Json(new NotificationResult() { Title = "Failed", Text = ex.Message, NotificationType = NotificationType.Error });
}
}
@ -176,31 +177,20 @@ namespace NzbDrone.Web.Controllers
}
//Root Directory
[HttpPost]
[JsonErrorFilter("Can't add root folder")]
public JsonResult SaveRootDir(string path)
{
if (String.IsNullOrWhiteSpace(path))
return new JsonResult { Data = "failed" };
NotificationResult.Error("Can't add root folder", "Path can not be empty");
//Don't let a user add a rootDir that is the same as their SABnzbd TV Directory
if (path.Equals(_configProvider.SabDropDirectory, StringComparison.InvariantCultureIgnoreCase))
return new JsonResult { Data = "failed" };
try
{
_rootFolderProvider.Add(new RootDir { Path = path });
}
catch (Exception ex)
{
Logger.Debug("Failed to save Root Dir");
Logger.DebugException(ex.Message, ex);
NotificationResult.Error("Can't add root folder", "Path can not be same as sab folder.");
return new JsonResult { Data = "failed" };
}
_rootFolderProvider.Add(new RootDir { Path = path });
return new JsonResult { Data = "ok" };
return NotificationResult.Info("Root Folder saved", "Root foler saved successfully.");
}
[HttpGet]

@ -0,0 +1,24 @@
using System.Linq;
using System.Web.Mvc;
using NzbDrone.Web.Models;
namespace NzbDrone.Web.Filters
{
public class JsonErrorFilter : FilterAttribute, IExceptionFilter
{
private readonly string _errorTitle;
public JsonErrorFilter(string errorTitle)
{
_errorTitle = errorTitle;
}
public void OnException(ExceptionContext filterContext)
{
filterContext.Result = NotificationResult.Error(_errorTitle, filterContext.Exception.Message);
filterContext.ExceptionHandled = true;
}
}
}

@ -1,3 +1,5 @@
using System.Web.Mvc;
namespace NzbDrone.Web.Models
{
public class NotificationResult
@ -7,13 +9,31 @@ namespace NzbDrone.Web.Models
Text = string.Empty;
}
public bool IsMessage { get { return true; } }
public string Title { get; set; }
public string Text { get; set; }
public NotificationType NotificationType { get; set; }
public static JsonResult Info(string title, string text)
{
return GetJsonResult(NotificationType.Error, title, text);
}
public static JsonResult Error(string title, string text)
{
return GetJsonResult(NotificationType.Error, title, text);
}
public static JsonResult GetJsonResult(NotificationType notificationType, string title, string text)
{
return new JsonResult
{
Data = new NotificationResult { NotificationType = notificationType, Title = title, Text = text },
ContentType = null,
ContentEncoding = null,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
}
public enum NotificationType

@ -199,6 +199,7 @@
</Compile>
<Compile Include="App_Start\EntityFramework.SqlServerCompact.cs" />
<Compile Include="App_Start\MiniProfiler.cs" />
<Compile Include="Filters\JsonErrorFilter.cs" />
<Compile Include="Controllers\CommandController.cs" />
<Compile Include="Controllers\DirectoryController.cs" />
<Compile Include="Controllers\EpisodeController.cs" />

@ -12,25 +12,24 @@
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
jqXHR.success(function (data) {
//Check if the response is a message type,
if (data.IsMessage) {
if (data.NotificationType === 0) {
$.gritter.add({
title: data.Title,
text: data.Text,
image: '../../content/images/success.png',
class_name: 'gritter-success'
});
}
else {
$.gritter.add({
title: data.Title,
text: data.Text,
image: '../../content/images/error.png',
class_name: 'gritter-fail'
});
}
if (data.NotificationType === 0) {
$.gritter.add({
title: data.Title,
text: data.Text,
image: '../../content/images/success.png',
class_name: 'gritter-success'
});
}
else if (data.NotificationType === 1) {
$.gritter.add({
title: data.Title,
text: data.Text,
image: '../../content/images/error.png',
class_name: 'gritter-fail',
time: 10000
});
}
});
jqXHR.error(function (xhr, ajaxOptions, thrownError) {
@ -40,7 +39,8 @@
title: 'Request failed',
text: this.url,
image: '../../content/images/error.png',
class_name: 'gritter-fail'
class_name: 'gritter-fail',
time: 10000
});
}
});

Loading…
Cancel
Save