diff --git a/NzbDrone.Api/REST/RestModule.cs b/NzbDrone.Api/REST/RestModule.cs index c5e18447e..b494206a0 100644 --- a/NzbDrone.Api/REST/RestModule.cs +++ b/NzbDrone.Api/REST/RestModule.cs @@ -68,8 +68,15 @@ protected Func GetResourceById Get[ID_ROUTE] = options => { ValidateId(options.Id); - var resource = GetResourceById((int)options.Id); - return resource.AsResponse(); + try + { + var resource = GetResourceById((int)options.Id); + return resource.AsResponse(); + } + catch (ModelNotFoundException) + { + return new NotFoundResponse(); + } }; } } diff --git a/NzbDrone.Core.Test/Datastore/BasicRepositoryFixture.cs b/NzbDrone.Core.Test/Datastore/BasicRepositoryFixture.cs index ceba7f125..0d443f729 100644 --- a/NzbDrone.Core.Test/Datastore/BasicRepositoryFixture.cs +++ b/NzbDrone.Core.Test/Datastore/BasicRepositoryFixture.cs @@ -84,7 +84,7 @@ public void single_or_default_on_empty_table_should_return_null() [Test] public void getting_model_with_invalid_id_should_throw() { - Assert.Throws(() => Subject.Get(12)); + Assert.Throws(() => Subject.Get(12)); } diff --git a/NzbDrone.Core.Test/Qualities/QualitySizeRepositoryFixture.cs b/NzbDrone.Core.Test/Qualities/QualitySizeRepositoryFixture.cs index d42fd9856..b5a22641d 100644 --- a/NzbDrone.Core.Test/Qualities/QualitySizeRepositoryFixture.cs +++ b/NzbDrone.Core.Test/Qualities/QualitySizeRepositoryFixture.cs @@ -1,5 +1,6 @@ using System; using NUnit.Framework; +using NzbDrone.Core.Datastore; using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Qualities; using NzbDrone.Core.Test.Framework; @@ -31,7 +32,7 @@ public void should_get_quality_size_by_id() public void should_throw_with_id_if_not_exist() { var id = 123; - Assert.Throws(()=> Subject.GetByQualityId(id)).Message.Contains(id.ToString()); + Assert.Throws(()=> Subject.GetByQualityId(id)).Message.Contains(id.ToString()); } } diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index 46c2b32f5..bb49496ef 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -4,6 +4,7 @@ using System.Linq.Expressions; using Marr.Data; using Marr.Data.QGen; +using NzbDrone.Common.EnsureThat; using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore.Events; using NzbDrone.Common; @@ -73,7 +74,14 @@ public int Count() public TModel Get(int id) { - return DataMapper.Query().Single(c => c.Id == id); + var model = DataMapper.Query().SingleOrDefault(c => c.Id == id); + + if (model == null) + { + throw new ModelNotFoundException(typeof(TModel), id); + } + + return model; } public IEnumerable Get(IEnumerable ids) diff --git a/NzbDrone.Core/Datastore/ModelNotFoundException.cs b/NzbDrone.Core/Datastore/ModelNotFoundException.cs new file mode 100644 index 000000000..8241fd344 --- /dev/null +++ b/NzbDrone.Core/Datastore/ModelNotFoundException.cs @@ -0,0 +1,14 @@ +using System; +using NzbDrone.Common.Exceptions; + +namespace NzbDrone.Core.Datastore +{ + public class ModelNotFoundException : NzbDroneException + { + public ModelNotFoundException(Type modelType, int modelId) + : base("{0} with ID {1} does not exist", modelType.Name, modelId) + { + + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 9ded5f15a..1ec1e37dc 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -228,6 +228,7 @@ + diff --git a/NzbDrone.Core/Qualities/QualitySizeRepository.cs b/NzbDrone.Core/Qualities/QualitySizeRepository.cs index c7f70f9e0..f8ae8d929 100644 --- a/NzbDrone.Core/Qualities/QualitySizeRepository.cs +++ b/NzbDrone.Core/Qualities/QualitySizeRepository.cs @@ -26,7 +26,7 @@ public QualitySize GetByQualityId(int qualityId) } catch (InvalidOperationException e) { - throw new InvalidOperationException("Sequence contains no element with qualityId = " + qualityId.ToString()); + throw new ModelNotFoundException(typeof(QualitySize), qualityId); } } } diff --git a/NzbDrone.Integration.Test/SeriesIntegrationTest.cs b/NzbDrone.Integration.Test/SeriesIntegrationTest.cs index 7be3233c7..33c7b40d2 100644 --- a/NzbDrone.Integration.Test/SeriesIntegrationTest.cs +++ b/NzbDrone.Integration.Test/SeriesIntegrationTest.cs @@ -1,8 +1,6 @@ -using System.IO; -using System.Net; +using System.Net; using FluentAssertions; using NUnit.Framework; -using NzbDrone.Api.RootFolders; using NzbDrone.Api.Series; using System.Linq; @@ -60,10 +58,8 @@ public void wrong_slug_should_return_404() } [Test] - [Ignore] public void invalid_id_should_return_404() { - //TODO: fix Series.Get(99, HttpStatusCode.NotFound); } }