From b8ac694fc43ec993cc88d4d261ce0bbaf29ab450 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Wed, 18 Jan 2012 19:58:32 -0800 Subject: [PATCH] added JsonErrorFilter to automatically handle failing ajax calls. --- .../Controllers/AddSeriesController.cs | 24 ++++-------- NzbDrone.Web/Filters/JsonErrorFilter.cs | 24 ++++++++++++ NzbDrone.Web/Models/NotificationResult.cs | 24 +++++++++++- NzbDrone.Web/NzbDrone.Web.csproj | 1 + NzbDrone.Web/Scripts/NzbDrone/Notification.js | 38 +++++++++---------- 5 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 NzbDrone.Web/Filters/JsonErrorFilter.cs diff --git a/NzbDrone.Web/Controllers/AddSeriesController.cs b/NzbDrone.Web/Controllers/AddSeriesController.cs index 9c4d73555..c177ef436 100644 --- a/NzbDrone.Web/Controllers/AddSeriesController.cs +++ b/NzbDrone.Web/Controllers/AddSeriesController.cs @@ -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] diff --git a/NzbDrone.Web/Filters/JsonErrorFilter.cs b/NzbDrone.Web/Filters/JsonErrorFilter.cs new file mode 100644 index 000000000..289858ec7 --- /dev/null +++ b/NzbDrone.Web/Filters/JsonErrorFilter.cs @@ -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; + } + + + } +} \ No newline at end of file diff --git a/NzbDrone.Web/Models/NotificationResult.cs b/NzbDrone.Web/Models/NotificationResult.cs index e2c49e93a..223b64f0a 100644 --- a/NzbDrone.Web/Models/NotificationResult.cs +++ b/NzbDrone.Web/Models/NotificationResult.cs @@ -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 diff --git a/NzbDrone.Web/NzbDrone.Web.csproj b/NzbDrone.Web/NzbDrone.Web.csproj index df16431a0..b0205517f 100644 --- a/NzbDrone.Web/NzbDrone.Web.csproj +++ b/NzbDrone.Web/NzbDrone.Web.csproj @@ -199,6 +199,7 @@ + diff --git a/NzbDrone.Web/Scripts/NzbDrone/Notification.js b/NzbDrone.Web/Scripts/NzbDrone/Notification.js index 2172503c1..d5697be6e 100644 --- a/NzbDrone.Web/Scripts/NzbDrone/Notification.js +++ b/NzbDrone.Web/Scripts/NzbDrone/Notification.js @@ -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 }); } });