-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CSHARP-5305: Improve error message when custom LINQ extension method is executed client side. #1547
base: main
Are you sure you want to change the base?
Conversation
…is executed client side.
@@ -42,7 +42,7 @@ internal static HashSet<T> ToHashSet<T>(this IEnumerable<T> source) | |||
/// <returns>Only meant to be used in Update specifications.</returns> | |||
public static TSource AllElements<TSource>(this IEnumerable<TSource> source) | |||
{ | |||
throw new NotSupportedException("This method is not functional. It is only usable in conjunction with MongoDB."); | |||
throw new NotSupportedException($"{nameof(AllElements)} can only be used in LINQ queries executed server-side."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this error message good enough or can you think of even better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO it's good enough.
@@ -862,7 +862,7 @@ public static IEnumerable<TResult> TopN<TSource, TKey, TResult>( | |||
/// <returns>The filtered results.</returns> | |||
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, int limit) | |||
{ | |||
throw new NotSupportedException("This method is not functional. It is only usable in conjunction with MongoDB."); | |||
throw new NotSupportedException($"{nameof(Where)} can only be used in LINQ queries executed server-side."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can move relace by single helper method to throw:
private void ThrowNotSupportedException([CallerMemberName]string callerName = null)
{
throw new NotSupportedException($"{callerName} can only be used in LINQ queries executed server-side.");
}
So each method could be converted to simple expression body method:
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, int limit)
=> ThrowNotSupportedException()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think losing "MongoDB" is a step backward as no other LINQ providers will be able to translate this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I will include "MongoDB" in the error message.
{ | ||
public static Exception CreateNotSupportedException([CallerMemberName] string methodName = null) | ||
{ | ||
throw new NotSupportedException($"{methodName} can only be used in MongoDB LINQ queries executed server-side."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we return it instead of throwing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
When I renamed this method from ThrowNotSupportedException
to CreateNotSupportedException
I missed changing this line!
{ | ||
public static Exception CreateNotSupportedException([CallerMemberName] string methodName = null) | ||
{ | ||
throw new NotSupportedException($"{methodName} can only be used in MongoDB LINQ queries executed server-side."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
When I renamed this method from ThrowNotSupportedException
to CreateNotSupportedException
I missed changing this line!
@@ -32,7 +33,7 @@ public static class DateTimeExtensions | |||
/// <returns>The resulting DateTime.</returns> | |||
public static DateTime Add(this DateTime @this, long value, DateTimeUnit unit) | |||
{ | |||
throw new InvalidOperationException("This DateTime.Add method is only intended to be used in LINQ queries."); | |||
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is switching from throwing InvalidOperationException
to throwing NotSupportedException
a breaking change?
No description provided.