Skip to content

Commit

Permalink
CSHARP-5305: Improve error message when custom LINQ extension method …
Browse files Browse the repository at this point in the history
…is executed client side.
  • Loading branch information
rstam committed Nov 22, 2024
1 parent 555c1fa commit 42e08b5
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 117 deletions.
53 changes: 27 additions & 26 deletions src/MongoDB.Driver/Linq/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

using System;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;

namespace MongoDB.Driver.Linq
{
Expand All @@ -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();
}

/// <summary>
Expand All @@ -46,7 +47,7 @@ public static DateTime Add(this DateTime @this, long value, DateTimeUnit unit)
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime Add(this DateTime @this, long value, DateTimeUnit unit, string timezone)
{
throw new InvalidOperationException("This DateTime.Add method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -59,7 +60,7 @@ public static DateTime Add(this DateTime @this, long value, DateTimeUnit unit, s
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime Add(this DateTime @this, TimeSpan value, string timezone)
{
throw new InvalidOperationException("This DateTime.Add method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -72,7 +73,7 @@ public static DateTime Add(this DateTime @this, TimeSpan value, string timezone)
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime AddDays(this DateTime @this, double value, string timezone)
{
throw new InvalidOperationException("This DateTime.AddDays method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -85,7 +86,7 @@ public static DateTime AddDays(this DateTime @this, double value, string timezon
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime AddHours(this DateTime @this, double value, string timezone)
{
throw new InvalidOperationException("This DateTime.AddHours method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -98,7 +99,7 @@ public static DateTime AddHours(this DateTime @this, double value, string timezo
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime AddMilliseconds(this DateTime @this, double value, string timezone)
{
throw new InvalidOperationException("This DateTime.AddMilliseconds method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -111,7 +112,7 @@ public static DateTime AddMilliseconds(this DateTime @this, double value, string
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime AddMinutes(this DateTime @this, double value, string timezone)
{
throw new InvalidOperationException("This DateTime.AddMinutes method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -124,7 +125,7 @@ public static DateTime AddMinutes(this DateTime @this, double value, string time
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime AddMonths(this DateTime @this, int value, string timezone)
{
throw new InvalidOperationException("This DateTime.AddMonths method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -135,7 +136,7 @@ public static DateTime AddMonths(this DateTime @this, int value, string timezone
/// <returns>The resulting DateTime.</returns>
public static DateTime AddQuarters(this DateTime @this, int value)
{
throw new InvalidOperationException("This DateTime.AddQuarters method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -148,7 +149,7 @@ public static DateTime AddQuarters(this DateTime @this, int value)
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime AddQuarters(this DateTime @this, int value, string timezone)
{
throw new InvalidOperationException("This DateTime.AddQuarters method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -161,7 +162,7 @@ public static DateTime AddQuarters(this DateTime @this, int value, string timezo
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime AddSeconds(this DateTime @this, double value, string timezone)
{
throw new InvalidOperationException("This DateTime.AddSeconds method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -172,7 +173,7 @@ public static DateTime AddSeconds(this DateTime @this, double value, string time
/// <returns>The resulting DateTime.</returns>
public static DateTime AddWeeks(this DateTime @this, int value)
{
throw new InvalidOperationException("The DateTime.AddWeeks method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -185,7 +186,7 @@ public static DateTime AddWeeks(this DateTime @this, int value)
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime AddWeeks(this DateTime @this, int value, string timezone)
{
throw new InvalidOperationException("This DateTime.AddWeeks method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -198,7 +199,7 @@ public static DateTime AddWeeks(this DateTime @this, int value, string timezone)
/// <remarks>See the server documentation for $dateAdd for information on timezones in MongoDB.</remarks>
public static DateTime AddYears(this DateTime @this, int value, string timezone)
{
throw new InvalidOperationException("This DateTime.AddYears method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -210,7 +211,7 @@ public static DateTime AddYears(this DateTime @this, int value, string timezone)
/// <returns>The result.</returns>
public static long Subtract(this DateTime @this, DateTime startDate, DateTimeUnit unit)
{
throw new InvalidOperationException("This DateTime.Subtract method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -224,7 +225,7 @@ public static long Subtract(this DateTime @this, DateTime startDate, DateTimeUni
/// <remarks>See the server documentation for $dateDiff for information on timezones in MongoDB.</remarks>
public static long Subtract(this DateTime @this, DateTime startDate, DateTimeUnit unit, string timezone)
{
throw new InvalidOperationException("This DateTime.Subtract method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -237,7 +238,7 @@ public static long Subtract(this DateTime @this, DateTime startDate, DateTimeUni
/// <remarks>See the server documentation for $dateDiff for information on timezones in MongoDB.</remarks>
public static TimeSpan Subtract(this DateTime @this, DateTime value, string timezone)
{
throw new InvalidOperationException("This DateTime.Subtract method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -250,7 +251,7 @@ public static TimeSpan Subtract(this DateTime @this, DateTime value, string time
/// <remarks>See the server documentation for $dateSubtract for information on timezones in MongoDB.</remarks>
public static DateTime Subtract(this DateTime @this, TimeSpan value, string timezone)
{
throw new InvalidOperationException("This DateTime.Subtract method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -262,7 +263,7 @@ public static DateTime Subtract(this DateTime @this, TimeSpan value, string time
/// <returns>The resulting DateTime.</returns>
public static DateTime Subtract(this DateTime @this, long value, DateTimeUnit unit)
{
throw new InvalidOperationException("This DateTime.Subtract method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -276,7 +277,7 @@ public static DateTime Subtract(this DateTime @this, long value, DateTimeUnit un
/// <remarks>See the server documentation for $dateSubtract for information on timezones in MongoDB.</remarks>
public static DateTime Subtract(this DateTime @this, long value, DateTimeUnit unit, string timezone)
{
throw new InvalidOperationException("This DateTime.Subtract method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -288,7 +289,7 @@ public static DateTime Subtract(this DateTime @this, long value, DateTimeUnit un
/// <returns>The DateTime value converted to a string.</returns>
public static string ToString(this DateTime @this, string format, string timezone)
{
throw new InvalidOperationException("This DateTime.ToString method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -299,7 +300,7 @@ public static string ToString(this DateTime @this, string format, string timezon
/// <returns>The resulting DateTime.</returns>
public static DateTime Truncate(this DateTime @this, DateTimeUnit unit)
{
throw new InvalidOperationException("This DateTime.Truncate method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -311,7 +312,7 @@ public static DateTime Truncate(this DateTime @this, DateTimeUnit unit)
/// <returns>The resulting DateTime.</returns>
public static DateTime Truncate(this DateTime @this, DateTimeUnit unit, long binSize)
{
throw new InvalidOperationException("This DateTime.Truncate method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -325,7 +326,7 @@ public static DateTime Truncate(this DateTime @this, DateTimeUnit unit, long bin
/// <remarks>See the server documentation for $dateTrunc for information on timezones in MongoDB.</remarks>
public static DateTime Truncate(this DateTime @this, DateTimeUnit unit, long binSize, string timezone)
{
throw new InvalidOperationException("This DateTime.Truncate method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -335,7 +336,7 @@ public static DateTime Truncate(this DateTime @this, DateTimeUnit unit, long bin
/// <returns>The week number of a specified DateTime value.</returns>
public static int Week(this DateTime @this)
{
throw new InvalidOperationException("This DateTime.Week method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}

/// <summary>
Expand All @@ -346,7 +347,7 @@ public static int Week(this DateTime @this)
/// <returns>The week number of a specified DateTime value.</returns>
public static int Week(this DateTime @this, string timezone)
{
throw new InvalidOperationException("This DateTime.Week method is only intended to be used in LINQ queries.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Runtime.CompilerServices;

namespace MongoDB.Driver.Linq.Linq3Implementation.Misc
{
internal static class CustomLinqExtensionMethodHelper
{
public static Exception CreateNotSupportedException([CallerMemberName] string methodName = null)
{
return new NotSupportedException($"{methodName} can only be used in MongoDB LINQ queries executed server-side.");
}
}
}
8 changes: 2 additions & 6 deletions src/MongoDB.Driver/Linq/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;

namespace MongoDB.Driver.Linq
{
Expand All @@ -36,7 +32,7 @@ public static class LinqExtensions
/// </returns>
public static bool Inject<TDocument>(this FilterDefinition<TDocument> filter)
{
throw new InvalidOperationException("The LinqExtensions.Inject method is only intended to be used in LINQ Where clauses.");
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
}
}
}
Loading

0 comments on commit 42e08b5

Please sign in to comment.