Skip to content

Commit b92934e

Browse files
committed
Prefer throw helper for missing settings
1 parent 3258e99 commit b92934e

File tree

6 files changed

+41
-17
lines changed

6 files changed

+41
-17
lines changed

src/Elastic.Clients.Elasticsearch/Api/BulkRequest.cs

+20-8
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ public void Serialize(Stream stream, IElasticsearchClientSettings settings, Seri
3434
foreach (var op in Operations)
3535
{
3636
if (op is not IStreamSerializable serializable)
37-
throw new InvalidOperationException("");
37+
{
38+
ThrowHelper.ThrowInvalidOperationForBulkWhenNotIStreamSerializable();
39+
return;
40+
}
3841

3942
op.PrepareIndex(index);
4043

41-
serializable.Serialize(stream, settings, formatting);
44+
serializable.Serialize(stream, settings, SerializationFormatting.None);
4245
stream.WriteByte((byte)'\n');
4346
}
4447
}
@@ -53,11 +56,14 @@ public async Task SerializeAsync(Stream stream, IElasticsearchClientSettings set
5356
foreach (var op in Operations)
5457
{
5558
if (op is not IStreamSerializable serializable)
56-
throw new InvalidOperationException("");
59+
{
60+
ThrowHelper.ThrowInvalidOperationForBulkWhenNotIStreamSerializable();
61+
return;
62+
}
5763

5864
op.PrepareIndex(index);
5965

60-
await serializable.SerializeAsync(stream, settings, formatting).ConfigureAwait(false);
66+
await serializable.SerializeAsync(stream, settings, SerializationFormatting.None).ConfigureAwait(false);
6167
stream.WriteByte((byte)'\n');
6268
}
6369
}
@@ -200,11 +206,14 @@ public void Serialize(Stream stream, IElasticsearchClientSettings settings, Seri
200206
foreach (var op in _operations)
201207
{
202208
if (op is not IStreamSerializable serializable)
203-
throw new InvalidOperationException("");
209+
{
210+
ThrowHelper.ThrowInvalidOperationForBulkWhenNotIStreamSerializable();
211+
return;
212+
}
204213

205214
op.PrepareIndex(index);
206215

207-
serializable.Serialize(stream, settings, formatting);
216+
serializable.Serialize(stream, settings, SerializationFormatting.None);
208217
stream.WriteByte((byte)'\n');
209218
}
210219
}
@@ -219,11 +228,14 @@ public async Task SerializeAsync(Stream stream, IElasticsearchClientSettings set
219228
foreach (var op in _operations)
220229
{
221230
if (op is not IStreamSerializable serializable)
222-
throw new InvalidOperationException("");
231+
{
232+
ThrowHelper.ThrowInvalidOperationForBulkWhenNotIStreamSerializable();
233+
return;
234+
}
223235

224236
op.PrepareIndex(index);
225237

226-
await serializable.SerializeAsync(stream, settings, formatting).ConfigureAwait(false);
238+
await serializable.SerializeAsync(stream, settings, SerializationFormatting.None).ConfigureAwait(false);
227239
stream.WriteByte((byte)'\n');
228240
}
229241
}

src/Elastic.Clients.Elasticsearch/Core/Exceptions/ThrowHelper.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static class ThrowHelper
1515

1616
[MethodImpl(MethodImplOptions.NoInlining)]
1717
internal static void ThrowUnknownTaggedUnionVariantJsonException(string variantTag, Type interfaceType) =>
18-
throw new JsonException($"Encounted an unsupported variant tag '{variantTag}' on '{SimplifiedFullName(interfaceType)}', which could not be deserialised.");
18+
throw new JsonException($"Encountered an unsupported variant tag '{variantTag}' on '{SimplifiedFullName(interfaceType)}', which could not be deserialized.");
1919

2020
[MethodImpl(MethodImplOptions.NoInlining)]
2121
internal static void ThrowInvalidOperationException(string message) =>
@@ -25,4 +25,10 @@ internal static void ThrowInvalidOperationException(string message) =>
2525
#pragma warning disable IDE0057 // Use range operator
2626
private static string SimplifiedFullName(Type type) => type.FullName.Substring(30);
2727
#pragma warning restore IDE0057 // Use range operator
28+
29+
[MethodImpl(MethodImplOptions.NoInlining)]
30+
internal static void ThrowJsonExceptionForMissingSettings() => throw new JsonException("Unable to retrieve client settings for JsonSerializerOptions.");
31+
32+
[MethodImpl(MethodImplOptions.NoInlining)]
33+
internal static void ThrowInvalidOperationForBulkWhenNotIStreamSerializable() => throw new InvalidOperationException("Operation must implement IStreamSerializable.");
2834
}

src/Elastic.Clients.Elasticsearch/Core/Fields/FieldValue.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public override void Write(Utf8JsonWriter writer, FieldValue value, JsonSerializ
374374
else if (value.TryGetComposite(out var objectValue))
375375
{
376376
if (!options.TryGetClientSettings(out var settings))
377-
throw new JsonException("Unable to retrieve ElasticsearchClientSettings in order to access source serializer.");
377+
ThrowHelper.ThrowJsonExceptionForMissingSettings();
378378

379379
SourceSerialization.Serialize(objectValue, objectValue.GetType(), writer, settings);
380380
}

src/Elastic.Clients.Elasticsearch/Types/Core/Bulk/BulkOperationsCollection.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,12 @@ public void Serialize(Stream stream, IElasticsearchClientSettings settings, Seri
235235
foreach (var op in this)
236236
{
237237
if (op is not IStreamSerializable serializable)
238-
throw new InvalidOperationException("");
238+
{
239+
ThrowHelper.ThrowInvalidOperationForBulkWhenNotIStreamSerializable();
240+
return;
241+
}
239242

240-
serializable.Serialize(stream, settings, formatting);
243+
serializable.Serialize(stream, settings, SerializationFormatting.None);
241244
stream.WriteByte((byte)'\n');
242245
}
243246
}
@@ -247,9 +250,12 @@ public async Task SerializeAsync(Stream stream, IElasticsearchClientSettings set
247250
foreach (var op in this)
248251
{
249252
if (op is not IStreamSerializable serializable)
250-
throw new InvalidOperationException("");
253+
{
254+
ThrowHelper.ThrowInvalidOperationForBulkWhenNotIStreamSerializable();
255+
return;
256+
}
251257

252-
await serializable.SerializeAsync(stream, settings, formatting).ConfigureAwait(false);
258+
await serializable.SerializeAsync(stream, settings, SerializationFormatting.None).ConfigureAwait(false);
253259
stream.WriteByte((byte)'\n');
254260
}
255261
}

src/Elastic.Clients.Elasticsearch/Types/Mapping/Properties.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal sealed class PropertiesConverter : JsonConverter<Properties>
2727
public override Properties? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2828
{
2929
if (!options.TryGetClientSettings(out var settings))
30-
throw new JsonException("Unable to retrive client settings during properties deserialization.");
30+
ThrowHelper.ThrowJsonExceptionForMissingSettings();
3131

3232
if (reader.TokenType != JsonTokenType.StartObject)
3333
throw new JsonException("Expected start object token.");
@@ -51,7 +51,7 @@ internal sealed class PropertiesConverter : JsonConverter<Properties>
5151
public override void Write(Utf8JsonWriter writer, Properties value, JsonSerializerOptions options)
5252
{
5353
if (!options.TryGetClientSettings(out var settings))
54-
throw new JsonException("Unable to retrive client settings during properties serialization.");
54+
ThrowHelper.ThrowJsonExceptionForMissingSettings();
5555

5656
if (value is null)
5757
{

src/Elastic.Clients.Elasticsearch/Types/SortOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public override SortOptions Read(ref Utf8JsonReader reader, Type typeToConvert,
7373
public override void Write(Utf8JsonWriter writer, SortOptions value, JsonSerializerOptions options)
7474
{
7575
if (!options.TryGetClientSettings(out var settings))
76-
throw new JsonException("Unable to retrieve IElasticsearchClientSettings.");
76+
ThrowHelper.ThrowJsonExceptionForMissingSettings();
7777

7878
string? fieldName = null;
7979

0 commit comments

Comments
 (0)