Skip to content

Commit 42e08b5

Browse files
committed
CSHARP-5305: Improve error message when custom LINQ extension method is executed client side.
1 parent 555c1fa commit 42e08b5

File tree

8 files changed

+147
-117
lines changed

8 files changed

+147
-117
lines changed

src/MongoDB.Driver/Linq/DateTimeExtensions.cs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
using System;
18+
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
1819

1920
namespace MongoDB.Driver.Linq
2021
{
@@ -32,7 +33,7 @@ public static class DateTimeExtensions
3233
/// <returns>The resulting DateTime.</returns>
3334
public static DateTime Add(this DateTime @this, long value, DateTimeUnit unit)
3435
{
35-
throw new InvalidOperationException("This DateTime.Add method is only intended to be used in LINQ queries.");
36+
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
3637
}
3738

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

341342
/// <summary>
@@ -346,7 +347,7 @@ public static int Week(this DateTime @this)
346347
/// <returns>The week number of a specified DateTime value.</returns>
347348
public static int Week(this DateTime @this, string timezone)
348349
{
349-
throw new InvalidOperationException("This DateTime.Week method is only intended to be used in LINQ queries.");
350+
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
350351
}
351352
}
352353
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Runtime.CompilerServices;
18+
19+
namespace MongoDB.Driver.Linq.Linq3Implementation.Misc
20+
{
21+
internal static class CustomLinqExtensionMethodHelper
22+
{
23+
public static Exception CreateNotSupportedException([CallerMemberName] string methodName = null)
24+
{
25+
return new NotSupportedException($"{methodName} can only be used in MongoDB LINQ queries executed server-side.");
26+
}
27+
}
28+
}

src/MongoDB.Driver/Linq/LinqExtensions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
using System;
17-
using System.Collections.Generic;
18-
using System.Linq;
19-
using System.Text;
20-
using System.Threading.Tasks;
16+
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
2117

2218
namespace MongoDB.Driver.Linq
2319
{
@@ -36,7 +32,7 @@ public static class LinqExtensions
3632
/// </returns>
3733
public static bool Inject<TDocument>(this FilterDefinition<TDocument> filter)
3834
{
39-
throw new InvalidOperationException("The LinqExtensions.Inject method is only intended to be used in LINQ Where clauses.");
35+
throw CustomLinqExtensionMethodHelper.CreateNotSupportedException();
4036
}
4137
}
4238
}

0 commit comments

Comments
 (0)