From 02cf23721a7d6ad2819ad3a5fb98ec2c48e5a63c Mon Sep 17 00:00:00 2001
From: Mark McDowall <markus.mcd5@gmail.com>
Date: Sun, 30 Sep 2012 17:05:16 -0700
Subject: [PATCH] Better client side error handling

---
 NzbDrone.Web/Controllers/AddSeriesController.cs  | 2 ++
 NzbDrone.Web/Controllers/CommandController.cs    | 2 ++
 NzbDrone.Web/Controllers/DirectoryController.cs  | 6 +++++-
 NzbDrone.Web/Controllers/SeriesController.cs     | 4 ++--
 NzbDrone.Web/Scripts/NzbDrone/Notification.js    | 3 +--
 NzbDrone.Web/Scripts/NzbDrone/addSeries.js       | 2 --
 NzbDrone.Web/Scripts/NzbDrone/qualitySettings.js | 7 -------
 NzbDrone.Web/Scripts/NzbDrone/seriesDetails.js   | 6 ------
 8 files changed, 12 insertions(+), 20 deletions(-)

diff --git a/NzbDrone.Web/Controllers/AddSeriesController.cs b/NzbDrone.Web/Controllers/AddSeriesController.cs
index cdb50d6ad..96adab086 100644
--- a/NzbDrone.Web/Controllers/AddSeriesController.cs
+++ b/NzbDrone.Web/Controllers/AddSeriesController.cs
@@ -110,6 +110,7 @@ namespace NzbDrone.Web.Controllers
         }
 
         [HttpPost]
+        [JsonErrorFilter]
         public JsonResult AddNewSeries(string path, string seriesName, int seriesId, int qualityProfileId, string startDate)
         {
             if (string.IsNullOrWhiteSpace(path) || String.Equals(path,"null",StringComparison.InvariantCultureIgnoreCase)) 
@@ -143,6 +144,7 @@ namespace NzbDrone.Web.Controllers
         }
 
         [HttpGet]
+        [JsonErrorFilter]
         public JsonResult LookupSeries(string term)
         {
             try
diff --git a/NzbDrone.Web/Controllers/CommandController.cs b/NzbDrone.Web/Controllers/CommandController.cs
index 971c46d69..5b7d8a5be 100644
--- a/NzbDrone.Web/Controllers/CommandController.cs
+++ b/NzbDrone.Web/Controllers/CommandController.cs
@@ -133,6 +133,7 @@ namespace NzbDrone.Web.Controllers
         }
 
         [HttpPost]
+        [JsonErrorFilter]
         public EmptyResult SaveSeasonIgnore(int seriesId, int seasonNumber, bool ignored)
         {
             _seasonProvider.SetIgnore(seriesId, seasonNumber, ignored);
@@ -140,6 +141,7 @@ namespace NzbDrone.Web.Controllers
         }
 
         [HttpPost]
+        [JsonErrorFilter]
         public EmptyResult SaveEpisodeIgnore(int episodeId, bool ignored)
         {
             _episodeProvider.SetEpisodeIgnore(episodeId, ignored);
diff --git a/NzbDrone.Web/Controllers/DirectoryController.cs b/NzbDrone.Web/Controllers/DirectoryController.cs
index 0d199a1a0..01e807618 100644
--- a/NzbDrone.Web/Controllers/DirectoryController.cs
+++ b/NzbDrone.Web/Controllers/DirectoryController.cs
@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Web.Mvc;
 using NzbDrone.Common;
+using NzbDrone.Web.Filters;
 
 namespace NzbDrone.Web.Controllers
 {
@@ -27,6 +28,7 @@ namespace NzbDrone.Web.Controllers
         }
 
         [HttpGet]
+        [JsonErrorFilter]
         public JsonResult GetDirectories(string term)
         {
             IEnumerable<string> dirs = null;
@@ -38,7 +40,6 @@ namespace NzbDrone.Web.Controllers
                 if (windowsSep > -1)
                 {
                     dirs = _diskProvider.GetDirectories(term.Substring(0, windowsSep + 1));
-
                 }
 
                 //Unix
@@ -55,6 +56,9 @@ namespace NzbDrone.Web.Controllers
                 //Swallow the exceptions so proper JSON is returned to the client (Empty results)
             }
 
+            if (dirs == null)
+                throw new Exception("A valid path was not provided");
+
             return Json(dirs, JsonRequestBehavior.AllowGet);
         }
     }
diff --git a/NzbDrone.Web/Controllers/SeriesController.cs b/NzbDrone.Web/Controllers/SeriesController.cs
index b539b8f06..9987eeec7 100644
--- a/NzbDrone.Web/Controllers/SeriesController.cs
+++ b/NzbDrone.Web/Controllers/SeriesController.cs
@@ -13,6 +13,7 @@ using NzbDrone.Core.Model;
 using NzbDrone.Core.Providers;
 using NzbDrone.Core.Repository;
 using NzbDrone.Core.Repository.Quality;
+using NzbDrone.Web.Filters;
 using NzbDrone.Web.Models;
 
 namespace NzbDrone.Web.Controllers
@@ -94,10 +95,9 @@ namespace NzbDrone.Web.Controllers
             return new EmptyResult();
         }
 
+        [JsonErrorFilter]
         public JsonResult LocalSearch(string term)
         {
-            //Get Results from the local DB and return
-
             var results = _seriesProvider.SearchForSeries(term).Select(s => new SeriesSearchResultModel
                                                                    {
                                                                        Id = s.SeriesId,
diff --git a/NzbDrone.Web/Scripts/NzbDrone/Notification.js b/NzbDrone.Web/Scripts/NzbDrone/Notification.js
index 7cad8c155..64519f6fe 100644
--- a/NzbDrone.Web/Scripts/NzbDrone/Notification.js
+++ b/NzbDrone.Web/Scripts/NzbDrone/Notification.js
@@ -37,10 +37,9 @@
             if (this.url.indexOf("/notification/Comet") === 0 || this.url.indexOf("/Health/Index") === 0 || this.url.indexOf("/signalr") === 0)
                 return;
 
-            alert("Status: " + textStatus + ", Error: " + thrownError);
             $.gritter.add({
                 title: 'Request failed',
-                text: this.url,
+                text: 'Url: ' + this.url + '<br/>Error: ' + thrownError,
                 image: '../../content/images/error.png',
                 class_name: 'gritter-fail',
                 time: 10000
diff --git a/NzbDrone.Web/Scripts/NzbDrone/addSeries.js b/NzbDrone.Web/Scripts/NzbDrone/addSeries.js
index 6f1c8fa71..b5601620d 100644
--- a/NzbDrone.Web/Scripts/NzbDrone/addSeries.js
+++ b/NzbDrone.Web/Scripts/NzbDrone/addSeries.js
@@ -46,7 +46,6 @@ $(".addExistingButton").live('click', function () {
         data: jQuery.param({ path: path, seriesName: title, seriesId: seriesId, qualityProfileId: qualityId, startDate: date }),
         error: function (req, status, error) {
             $(button).removeAttr('disabled');
-            alert("Sorry! We could not add " + path + " at this time. " + error);
         },
         success: function() {
             root.hide('highlight', 'fast');
@@ -137,7 +136,6 @@ $('#saveNewSeries').live('click', function () {
         data: jQuery.param({ path: path, seriesName: seriesTitle, seriesId: seriesId, qualityProfileId: qualityId, startDate: date }),
         error: function (req, status, error) {
             $('#saveNewSeries').removeAttr('disabled');
-            alert("Sorry! We could not add " + path + " at this time. " + error);
         },
         success: function () {
             $('#saveNewSeries').removeAttr('disabled');
diff --git a/NzbDrone.Web/Scripts/NzbDrone/qualitySettings.js b/NzbDrone.Web/Scripts/NzbDrone/qualitySettings.js
index f77751beb..03618b3c1 100644
--- a/NzbDrone.Web/Scripts/NzbDrone/qualitySettings.js
+++ b/NzbDrone.Web/Scripts/NzbDrone/qualitySettings.js
@@ -40,18 +40,11 @@ function sendToServer(id) {
         type: "POST",
         url: deleteQualityProfileUrl,
         data: jQuery.param({ profileId: id }),
-        error: function (req, status, error) {
-            alert("Sorry! We could not delete your Profile at this time. " + error);
-        },
         success: function (data, textStatus, jqXHR) {
             if (data == "ok") {
                 $("#profile_" + id).remove();
                 removeOption(id);
             }
-
-            else {
-                alert(data);
-            }
         }
     });
 }
diff --git a/NzbDrone.Web/Scripts/NzbDrone/seriesDetails.js b/NzbDrone.Web/Scripts/NzbDrone/seriesDetails.js
index 066f83924..9bf6447a6 100644
--- a/NzbDrone.Web/Scripts/NzbDrone/seriesDetails.js
+++ b/NzbDrone.Web/Scripts/NzbDrone/seriesDetails.js
@@ -111,9 +111,6 @@ function saveSeasonIgnore(seasonNumber, ignored) {
         type: "POST",
         url: saveSeasonIgnoreUrl,
         data: jQuery.param({ seriesId: seriesId, seasonNumber: seasonNumber, ignored: ignored }),
-        error: function (req, status, error) {
-            alert("Sorry! We could save the ignore settings for Series: " + seriesId + ", Season: " + seasonNumber + " at this time. " + error);
-        }
     });
 }
 
@@ -122,9 +119,6 @@ function saveEpisodeIgnore(episodeId, ignored) {
         type: "POST",
         url: saveEpisodeIgnoreUrl,
         data: jQuery.param({ episodeId: episodeId, ignored: ignored }),
-        error: function (req, status, error) {
-            alert("Sorry! We could save the ignore settings for Episode: " + episodeId + " at this time. " + error);
-        }
     });
 }