Skip to content

Commit b42dcd8

Browse files
committed
feat: QueryKitParsingException exception support
1 parent 828c9db commit b42dcd8

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace QueryKit.Exceptions;
2+
3+
public sealed class QueryKitParsingException : Exception
4+
{
5+
public QueryKitParsingException(string message)
6+
: base(message)
7+
{
8+
}
9+
}

QueryKit/Operators/ComparisonOperator.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static ComparisonOperator GetByOperatorString(string op, bool caseInsensi
6363
var comparisonOperator = List.FirstOrDefault(x => x.Operator() == op);
6464
if (comparisonOperator == null)
6565
{
66-
throw new Exception($"Operator {op} is not supported");
66+
throw new QueryKitParsingException($"Operator {op} is not supported");
6767
}
6868

6969
ComparisonOperator? newOperator = null;
@@ -162,7 +162,7 @@ public static ComparisonOperator GetByOperatorString(string op, bool caseInsensi
162162
}
163163

164164
return newOperator == null
165-
? throw new Exception($"Operator {op} is not supported")
165+
? throw new QueryKitParsingException($"Operator {op} is not supported")
166166
: newOperator!;
167167
}
168168

@@ -658,7 +658,7 @@ public override Expression GetExpression<T>(Expression left, Expression right, T
658658
return GetCollectionExpression(left, right, Expression.Equal, UsesAll);
659659
}
660660

661-
throw new Exception("DoesNotHaveType is only supported for collections");
661+
throw new QueryKitParsingException("DoesNotHaveType is only supported for collections");
662662
}
663663
}
664664

@@ -680,7 +680,7 @@ public override Expression GetExpression<T>(Expression left, Expression right, T
680680
return GetCollectionExpression(left, right, Expression.NotEqual, UsesAll);
681681
}
682682

683-
throw new Exception("DoesNotHaveType is only supported for collections");
683+
throw new QueryKitParsingException("DoesNotHaveType is only supported for collections");
684684
}
685685
}
686686

@@ -862,22 +862,22 @@ private Expression GetCountExpression(Expression left, Expression right, string
862862
var leftAsEnumerableType = left.Type.GetInterface(nameof(IEnumerable));
863863
if (leftAsEnumerableType == null)
864864
{
865-
throw new Exception("Left expression should be of type IEnumerable<T>");
865+
throw new QueryKitParsingException("Left expression should be of type IEnumerable<T>");
866866
}
867867

868868
var leftGenericType = left.Type.GetGenericArguments()[0];
869869
var rightType = right.Type;
870870

871871
if (rightType != typeof(int))
872872
{
873-
throw new Exception("The right expression should be of type int");
873+
throw new QueryKitParsingException("The right expression should be of type int");
874874
}
875875
var countMethod = typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static)
876876
.FirstOrDefault(m => m.Name == "Count" && m.GetParameters().Length == 1
877877
&& m.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>));
878878
if (countMethod == null)
879879
{
880-
throw new Exception("Count method not found");
880+
throw new QueryKitParsingException("Count method not found");
881881
}
882882

883883
var specificCountMethod = countMethod.MakeGenericMethod(leftGenericType);
@@ -886,10 +886,9 @@ private Expression GetCountExpression(Expression left, Expression right, string
886886
var comparisonMethod = typeof(Expression).GetMethod(methodName, new[] { typeof(Expression), typeof(Expression) });
887887
if (comparisonMethod == null)
888888
{
889-
throw new Exception($"Comparison method '{methodName}' not found");
889+
throw new QueryKitParsingException($"Comparison method '{methodName}' not found");
890890
}
891891

892-
// Invoke the comparison method
893892
return (Expression)comparisonMethod.Invoke(null, new object[] { countExpression, right });
894893
}
895894
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ If you want to capture errors to easily throw a `400`, you can add error handlin
402402
* A `SortParsingException` will be thrown if a property or operation is not recognized during sorting
403403
* A `QueryKitDbContextTypeException` will be thrown when trying to use a `DbContext` specific workflow without passing that context (e.g. SoundEx)
404404
* A `SoundsLikeNotImplementedException` will be thrown when trying to use `soundex` on a `DbContext` that doesn't have it implemented.
405+
* A `QueryKitParsingException` is a more generic error that will include specific details on a more granular error in the parsing pipeline.
405406
406407
## SoundEx
407408

0 commit comments

Comments
 (0)