Basic exception handling (#7)

This commit is contained in:
Biarity 2018-02-14 08:06:26 +10:00
parent 9d0ef79595
commit b625d15cb8
4 changed files with 75 additions and 3 deletions

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Sieve.Exceptions
{
public class SieveIncompatibleMethodException : Exception
{
public string MethodName { get; protected set; }
public Type ExpectedType { get; protected set; }
public Type ActualType { get; protected set; }
public SieveIncompatibleMethodException(
string methodName,
Type expectedType,
Type actualType,
string message)
: base(message)
{
MethodName = methodName;
ExpectedType = expectedType;
ActualType = actualType;
}
public SieveIncompatibleMethodException(
string methodName,
Type expectedType,
Type actualType,
string message,
Exception innerException)
: base(message, innerException)
{
MethodName = methodName;
ExpectedType = expectedType;
ActualType = actualType;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Sieve.Exceptions
{
public class SieveMethodNotFoundException : Exception
{
public string MethodName { get; protected set; }
public SieveMethodNotFoundException(string methodName, string message) : base (message)
{
MethodName = methodName;
}
public SieveMethodNotFoundException(string methodName, string message, Exception innerException) : base(message, innerException)
{
MethodName = methodName;
}
}
}

View File

@ -11,6 +11,7 @@ using Sieve.Extensions;
using System.ComponentModel;
using System.Collections;
using System.Linq.Expressions;
using Sieve.Exceptions;
namespace Sieve.Services
{
@ -294,7 +295,6 @@ namespace Sieve.Services
result = customMethod.Invoke(parent, parameters)
as IQueryable<TEntity>;
}
catch (ArgumentException) { } // name matched with custom emthod for a differnt type
catch (TargetParameterCountException)
{
if (optionalParameters != null)
@ -307,6 +307,18 @@ namespace Sieve.Services
throw;
}
}
catch (ArgumentException) // name matched with custom method for a differnt type
{
var expected = typeof(TEntity);
var actual = ((IQueryable)customMethod.ReturnParameter).ElementType;
throw new SieveIncompatibleMethodException(name, expected, actual,
$"{name} failed. Expected a custom method for type {expected} but only found for type {actual}");
}
}
else
{
throw new SieveMethodNotFoundException(name,
$"{name} not found.");
}
return result;

View File

@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>Sieve</id>
<version>1.3.9</version>
<version>1.3.91</version>
<title>Sieve</title>
<authors>Biarity</authors>
<owners>Biarity</owners>
@ -13,7 +13,7 @@
<description>
Sieve is a simple, clean, and extensible framework for .NET Core that adds sorting, filtering, and pagination functionality out of the box. Most common use case would be for serving ASP.NET Core GET queries. Documentation available on GitHub: https://github.com/Biarity/Sieve/
</description>
<releaseNotes>Fluent API v1 and case-insensitive operators</releaseNotes>
<releaseNotes>Custom method case-insensitivity</releaseNotes>
<copyright>Copyright 2018</copyright>
<tags>aspnetcore filter sort page paginate sieve search</tags>
<dependencies>