1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Getting model by invalid ID throws a more specific exception.

This commit is contained in:
Keivan Beigi 2013-07-09 15:06:30 -07:00
parent f3534de0c5
commit 54c36e9264
8 changed files with 38 additions and 11 deletions

View File

@ -68,8 +68,15 @@ protected Func<int, TResource> 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();
}
};
}
}

View File

@ -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<InvalidOperationException>(() => Subject.Get(12));
Assert.Throws<ModelNotFoundException>(() => Subject.Get(12));
}

View File

@ -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<InvalidOperationException>(()=> Subject.GetByQualityId(id)).Message.Contains(id.ToString());
Assert.Throws<ModelNotFoundException>(()=> Subject.GetByQualityId(id)).Message.Contains(id.ToString());
}
}

View File

@ -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<TModel>().Single(c => c.Id == id);
var model = DataMapper.Query<TModel>().SingleOrDefault(c => c.Id == id);
if (model == null)
{
throw new ModelNotFoundException(typeof(TModel), id);
}
return model;
}
public IEnumerable<TModel> Get(IEnumerable<int> ids)

View File

@ -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)
{
}
}
}

View File

@ -228,6 +228,7 @@
<Compile Include="Datastore\Migration\Framework\SQLiteMigrationHelper.cs" />
<Compile Include="Datastore\ModelBase.cs" />
<Compile Include="Datastore\BasicRepository.cs" />
<Compile Include="Datastore\ModelNotFoundException.cs" />
<Compile Include="Datastore\PagingSpec.cs" />
<Compile Include="Datastore\PagingSpecExtensions.cs" />
<Compile Include="Datastore\RelationshipExtensions.cs" />

View File

@ -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);
}
}
}

View File

@ -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);
}
}