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;
using NzbDrone.Core.Providers.Core; using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository; using NzbDrone.Core.Repository;
using NzbDrone.Web.Filters;
using NzbDrone.Web.Models; using NzbDrone.Web.Models;
namespace NzbDrone.Web.Controllers namespace NzbDrone.Web.Controllers
@ -176,31 +177,20 @@ namespace NzbDrone.Web.Controllers
} }
//Root Directory
[HttpPost] [HttpPost]
[JsonErrorFilter("Can't add root folder")]
public JsonResult SaveRootDir(string path) public JsonResult SaveRootDir(string path)
{ {
if (String.IsNullOrWhiteSpace(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 //Don't let a user add a rootDir that is the same as their SABnzbd TV Directory
if (path.Equals(_configProvider.SabDropDirectory, StringComparison.InvariantCultureIgnoreCase)) 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 }); _rootFolderProvider.Add(new RootDir { Path = path });
} return NotificationResult.Info("Root Folder saved", "Root foler saved successfully.");
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" };
} }
[HttpGet] [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 namespace NzbDrone.Web.Models
{ {
public class NotificationResult public class NotificationResult
@ -7,13 +9,31 @@ namespace NzbDrone.Web.Models
Text = string.Empty; Text = string.Empty;
} }
public bool IsMessage { get { return true; } }
public string Title { get; set; } public string Title { get; set; }
public string Text { get; set; } public string Text { get; set; }
public NotificationType NotificationType { 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 public enum NotificationType

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

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

Loading…
Cancel
Save