Getting model by invalid ID throws a more specific exception.
This commit is contained in:
parent
f3534de0c5
commit
54c36e9264
|
@ -68,8 +68,15 @@ namespace NzbDrone.Api.REST
|
|||
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();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ namespace NzbDrone.Core.Test.Datastore
|
|||
[Test]
|
||||
public void getting_model_with_invalid_id_should_throw()
|
||||
{
|
||||
Assert.Throws<InvalidOperationException>(() => Subject.Get(12));
|
||||
Assert.Throws<ModelNotFoundException>(() => Subject.Get(12));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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 @@ namespace NzbDrone.Core.Test.Qualities
|
|||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
|||
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 @@ namespace NzbDrone.Core.Datastore
|
|||
|
||||
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)
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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" />
|
||||
|
|
|
@ -26,7 +26,7 @@ namespace NzbDrone.Core.Qualities
|
|||
}
|
||||
catch (InvalidOperationException e)
|
||||
{
|
||||
throw new InvalidOperationException("Sequence contains no element with qualityId = " + qualityId.ToString());
|
||||
throw new ModelNotFoundException(typeof(QualitySize), qualityId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 @@ namespace NzbDrone.Integration.Test
|
|||
}
|
||||
|
||||
[Test]
|
||||
[Ignore]
|
||||
public void invalid_id_should_return_404()
|
||||
{
|
||||
//TODO: fix
|
||||
Series.Get(99, HttpStatusCode.NotFound);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue