Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

rstam
Copy link
Contributor

@rstam rstam commented Nov 16, 2024

No description provided.

@rstam rstam requested a review from BorisDog November 16, 2024 02:16
@rstam rstam requested a review from a team as a code owner November 16, 2024 02:16
@@ -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.");
Copy link
Contributor Author

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?

Copy link
Contributor

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.");
Copy link
Member

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()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Copy link
Member

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.

Copy link
Contributor Author

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.");
Copy link
Member

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?

Copy link
Contributor Author

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.");
Copy link
Contributor Author

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();
Copy link
Contributor Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants