diff --git a/src/NzbDrone.Core/Datastore/ModelConflictException.cs b/src/NzbDrone.Core/Datastore/ModelConflictException.cs
new file mode 100644
index 000000000..66c7f94ff
--- /dev/null
+++ b/src/NzbDrone.Core/Datastore/ModelConflictException.cs
@@ -0,0 +1,20 @@
+using System;
+using NzbDrone.Common.Exceptions;
+
+namespace NzbDrone.Core.Datastore
+{
+ public class ModelConflictException : NzbDroneException
+ {
+ public ModelConflictException(Type modelType, int modelId)
+ : base("{0} with ID {1} cannot be modified", modelType.Name, modelId)
+ {
+
+ }
+
+ public ModelConflictException(Type modelType, int modelId, string message)
+ : base("{0} with ID {1} {2}", modelType.Name, modelId, message)
+ {
+
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj
index 458de42e0..9e2475c9e 100644
--- a/src/NzbDrone.Core/NzbDrone.Core.csproj
+++ b/src/NzbDrone.Core/NzbDrone.Core.csproj
@@ -142,6 +142,7 @@
+
diff --git a/src/NzbDrone.Core/Tags/TagDetails.cs b/src/NzbDrone.Core/Tags/TagDetails.cs
index cd223f81c..c378fa48b 100644
--- a/src/NzbDrone.Core/Tags/TagDetails.cs
+++ b/src/NzbDrone.Core/Tags/TagDetails.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Linq;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Tags
@@ -10,5 +11,13 @@ namespace NzbDrone.Core.Tags
public List NotificationIds { get; set; }
public List RestrictionIds { get; set; }
public List DelayProfileIds { get; set; }
+
+ public bool InUse
+ {
+ get
+ {
+ return (SeriesIds.Any() || NotificationIds.Any() || RestrictionIds.Any() || DelayProfileIds.Any());
+ }
+ }
}
}
diff --git a/src/NzbDrone.Core/Tags/TagService.cs b/src/NzbDrone.Core/Tags/TagService.cs
index 6b5a63dc4..ddd07f409 100644
--- a/src/NzbDrone.Core/Tags/TagService.cs
+++ b/src/NzbDrone.Core/Tags/TagService.cs
@@ -1,5 +1,7 @@
+using System;
using System.Collections.Generic;
using System.Linq;
+using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Notifications;
using NzbDrone.Core.Profiles.Delay;
@@ -147,6 +149,12 @@ namespace NzbDrone.Core.Tags
public void Delete(int tagId)
{
+ var details = Details(tagId);
+ if (details.InUse)
+ {
+ throw new ModelConflictException(typeof(Tag), tagId, $"'{details.Label}' cannot be deleted since it's still in use");
+ }
+
_repo.Delete(tagId);
_eventAggregator.PublishEvent(new TagsUpdatedEvent());
}
diff --git a/src/Sonarr.Http/ErrorManagement/SonarrErrorPipeline.cs b/src/Sonarr.Http/ErrorManagement/SonarrErrorPipeline.cs
index ab2f8a21d..01bd5a068 100644
--- a/src/Sonarr.Http/ErrorManagement/SonarrErrorPipeline.cs
+++ b/src/Sonarr.Http/ErrorManagement/SonarrErrorPipeline.cs
@@ -3,6 +3,7 @@ using System.Data.SQLite;
using FluentValidation;
using Nancy;
using NLog;
+using NzbDrone.Core.Datastore;
using NzbDrone.Core.Exceptions;
using Sonarr.Http.Exceptions;
using Sonarr.Http.Extensions;
@@ -23,26 +24,20 @@ namespace Sonarr.Http.ErrorManagement
{
_logger.Trace("Handling Exception");
- var apiException = exception as ApiException;
-
- if (apiException != null)
+ if (exception is ApiException apiException)
{
_logger.Warn(apiException, "API Error");
return apiException.ToErrorResponse();
}
-
- var validationException = exception as ValidationException;
-
- if (validationException != null)
+
+ if (exception is ValidationException validationException)
{
_logger.Warn("Invalid request {0}", validationException.Message);
return validationException.Errors.AsResponse(HttpStatusCode.BadRequest);
}
- var clientException = exception as NzbDroneClientException;
-
- if (clientException != null)
+ if (exception is NzbDroneClientException clientException)
{
return new ErrorModel
{
@@ -51,9 +46,25 @@ namespace Sonarr.Http.ErrorManagement
}.AsResponse((HttpStatusCode)clientException.StatusCode);
}
- var sqLiteException = exception as SQLiteException;
+ if (exception is ModelNotFoundException notFoundException)
+ {
+ return new ErrorModel
+ {
+ Message = exception.Message,
+ Description = exception.ToString()
+ }.AsResponse(HttpStatusCode.NotFound);
+ }
+
+ if (exception is ModelConflictException conflictException)
+ {
+ return new ErrorModel
+ {
+ Message = exception.Message,
+ Description = exception.ToString()
+ }.AsResponse(HttpStatusCode.Conflict);
+ }
- if (sqLiteException != null)
+ if (exception is SQLiteException sqLiteException)
{
if (context.Request.Method == "PUT" || context.Request.Method == "POST")
{