1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 10:32:35 +01:00

added JsonErrorFilter to automatically handle failing ajax calls.

This commit is contained in:
kay.one 2012-01-18 19:58:32 -08:00
parent 7c6d745c86
commit b8ac694fc4
5 changed files with 73 additions and 38 deletions

View File

@ -9,6 +9,7 @@
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 @@ public JsonResult AddExistingSeries(string path, string seriesName, int seriesId
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 @@ public ActionResult QuickAdd()
}
//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" };
NotificationResult.Error("Can't add root folder", "Path can not be same as sab folder.");
try
{
_rootFolderProvider.Add(new RootDir { Path = path });
}
catch (Exception ex)
{
Logger.Debug("Failed to save Root Dir");
Logger.DebugException(ex.Message, ex);
return new JsonResult { Data = "failed" };
}
return new JsonResult { Data = "ok" };
return NotificationResult.Info("Root Folder saved", "Root foler saved successfully.");
}
[HttpGet]

View File

@ -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;
}
}
}

View File

@ -1,3 +1,5 @@
using System.Web.Mvc;
namespace NzbDrone.Web.Models
{
public class NotificationResult
@ -7,13 +9,31 @@ public NotificationResult()
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

View File

@ -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" />

View File

@ -12,8 +12,6 @@
$.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,
@ -22,15 +20,16 @@
class_name: 'gritter-success'
});
}
else {
else if (data.NotificationType === 1) {
$.gritter.add({
title: data.Title,
text: data.Text,
image: '../../content/images/error.png',
class_name: 'gritter-fail'
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
});
}
});