Skip to content

Commit 94ac52a

Browse files
committed
can now specify ThrowExceptions() on a per request basis, the build in extension methods that do not return an IResponse now set this so that they do not eat bad responses
1 parent 5eaa8da commit 94ac52a

File tree

7 files changed

+51
-21
lines changed

7 files changed

+51
-21
lines changed

src/Elasticsearch.Net/Configuration/RequestConfiguration.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public interface IRequestConfiguration
8181
/// Use the following client certificates to authenticate this single request
8282
/// </summary>
8383
X509CertificateCollection ClientCertificates { get; set; }
84+
85+
/// <summary>
86+
/// Instead of following a c/go like error checking on response.IsValid always throw an exception
87+
/// on the client when a call resulted in an exception on either the client or the Elasticsearch server.
88+
/// <para>Reasons for such exceptions could be search parser errors, index missing exceptions, etc...</para>
89+
/// </summary>
90+
bool ThrowExceptions { get; set; }
8491
}
8592

8693
public class RequestConfiguration : IRequestConfiguration
@@ -105,6 +112,7 @@ public class RequestConfiguration : IRequestConfiguration
105112
public string RunAs { get; set; }
106113

107114
public X509CertificateCollection ClientCertificates { get; set; }
115+
public bool ThrowExceptions { get; set; }
108116
}
109117

110118
public class RequestConfigurationDescriptor : IRequestConfiguration
@@ -125,6 +133,7 @@ public class RequestConfigurationDescriptor : IRequestConfiguration
125133
bool IRequestConfiguration.EnableHttpPipelining { get; set; } = true;
126134
string IRequestConfiguration.RunAs { get; set; }
127135
X509CertificateCollection IRequestConfiguration.ClientCertificates { get; set; }
136+
bool IRequestConfiguration.ThrowExceptions { get; set; }
128137

129138
public RequestConfigurationDescriptor(IRequestConfiguration config)
130139
{
@@ -202,6 +211,12 @@ public RequestConfigurationDescriptor DisablePing(bool? disable = true)
202211
return this;
203212
}
204213

214+
public RequestConfigurationDescriptor ThrowExceptions(bool throwExceptions = true)
215+
{
216+
Self.ThrowExceptions = throwExceptions;
217+
return this;
218+
}
219+
205220
public RequestConfigurationDescriptor DisableDirectStreaming(bool? disable = true)
206221
{
207222
Self.DisableDirectStreaming = disable;

src/Elasticsearch.Net/Transport/Pipeline/RequestData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class RequestData
4040
public string ProxyUsername { get; }
4141
public string ProxyPassword { get; }
4242
public bool DisableAutomaticProxyDetection { get; }
43+
public bool ThrowExceptions { get; }
4344

4445
public BasicAuthenticationCredentials BasicAuthorizationCredentials { get; }
4546
public IEnumerable<int> AllowedStatusCodes { get; }
@@ -81,6 +82,7 @@ private RequestData(
8182
this.Headers = global.Headers != null ? new NameValueCollection(global.Headers) : new NameValueCollection();
8283
this.RunAs = local?.RunAs;
8384
this.SkipDeserializationForStatusCodes = global?.SkipDeserializationForStatusCodes;
85+
this.ThrowExceptions = local?.ThrowExceptions ?? global.ThrowExceptions;
8486

8587
this.RequestTimeout = local?.RequestTimeout ?? global.RequestTimeout;
8688
this.PingTimeout =

src/Elasticsearch.Net/Transport/Transport.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private TResponse FinalizeResponse<TResponse>(RequestData requestData, IRequestP
176176
if (response?.ApiCall == null)
177177
pipeline.BadResponse(ref response, callDetails, requestData, clientException);
178178

179-
HandleElasticsearchClientException(clientException, response);
179+
HandleElasticsearchClientException(requestData, clientException, response);
180180
return response;
181181
}
182182

@@ -188,12 +188,12 @@ private static IApiCallDetails GetMostRecentCallDetails<TResponse>(TResponse res
188188
}
189189

190190

191-
private void HandleElasticsearchClientException(ElasticsearchClientException clientException, IElasticsearchResponse response)
191+
private void HandleElasticsearchClientException(RequestData data, Exception clientException, IElasticsearchResponse response)
192192
{
193193
if (clientException != null && response.ApiCall.OriginalException == null && response.ApiCall is ApiCallDetails a)
194194
a.OriginalException = clientException;
195195
this.Settings.OnRequestCompleted?.Invoke(response.ApiCall);
196-
if (clientException != null && this.Settings.ThrowExceptions) throw clientException;
196+
if (clientException != null && data.ThrowExceptions) throw clientException;
197197
}
198198

199199
private static void Ping(IRequestPipeline pipeline, Node node)

src/Nest/Document/Multiple/MultiGet/ElasticClient-GetMany.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ public static class GetManyExtensions
2525
public static IEnumerable<IMultiGetHit<T>> GetMany<T>(this IElasticClient client, IEnumerable<string> ids, IndexName index = null, TypeName type = null)
2626
where T : class
2727
{
28-
var result = client.MultiGet(s => s.GetMany<T>(ids).Index(index).Type(type));
28+
var result = client.MultiGet(s => s
29+
.RequestConfiguration(r=>r.ThrowExceptions())
30+
.GetMany<T>(ids)
31+
.Index(index)
32+
.Type(type)
33+
);
2934
return result.GetMany<T>(ids);
3035
}
3136

@@ -61,7 +66,13 @@ public static async Task<IEnumerable<IMultiGetHit<T>>> GetManyAsync<T>(
6166
this IElasticClient client, IEnumerable<string> ids, IndexName index = null, TypeName type = null, CancellationToken cancellationToken = default(CancellationToken))
6267
where T : class
6368
{
64-
var response = await client.MultiGetAsync(s => s.GetMany<T>(ids).Index(index).Type(type), cancellationToken).ConfigureAwait(false);
69+
var response = await client.MultiGetAsync(s => s
70+
.RequestConfiguration(r=>r.ThrowExceptions())
71+
.GetMany<T>(ids)
72+
.Index(index)
73+
.Type(type),
74+
cancellationToken
75+
) .ConfigureAwait(false);
6576
return response.GetMany<T>(ids);
6677
}
6778

src/Nest/Document/Multiple/MultiGet/ElasticClient-SourceMany.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@ public static class SourceManyExtensions
2828
public static IEnumerable<T> SourceMany<T>(this IElasticClient client, IEnumerable<string> ids, string index = null, string type = null)
2929
where T : class
3030
{
31-
var result = client.MultiGet(s => s.GetMany<T>(ids, (gs, i) =>
32-
{
33-
if (!string.IsNullOrEmpty(index))
34-
gs.Index(index);
35-
36-
if (!string.IsNullOrEmpty(type))
37-
gs.Type(type);
38-
39-
return gs;
40-
}));
41-
31+
var result = client.MultiGet(s => s
32+
.RequestConfiguration(r=>r.ThrowExceptions())
33+
.GetMany<T>(ids, (gs, i) => gs
34+
.Index(index)
35+
.Type(type)
36+
)
37+
);
4238
return result.SourceMany<T>(ids);
4339
}
4440

@@ -80,7 +76,13 @@ public static async Task<IEnumerable<T>> SourceManyAsync<T>(
8076
this IElasticClient client, IEnumerable<string> ids, string index = null, string type = null, CancellationToken cancellationToken = default(CancellationToken))
8177
where T : class
8278
{
83-
var response = await client.MultiGetAsync(s => s.GetMany<T>(ids, (gs, i) => gs.Index(index).Type(type)), cancellationToken).ConfigureAwait(false);
79+
var response = await client.MultiGetAsync(s => s
80+
.RequestConfiguration(r=>r.ThrowExceptions())
81+
.GetMany<T>(ids, (gs, i) => gs
82+
.Index(index)
83+
.Type(type)
84+
), cancellationToken)
85+
.ConfigureAwait(false);
8486
return response.SourceMany<T>(ids);
8587
}
8688

src/Nest/Indices/AliasManagement/GetAlias/ElasticClient-GetAliasesPointingToIndex.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static class AliasPointingToIndexExtensions
1313
/// <param name="index">The index name we want to know aliases of</param>
1414
public static IReadOnlyDictionary<string, AliasDefinition> GetAliasesPointingToIndex(this IElasticClient client, IndexName index)
1515
{
16-
var response = client.GetAlias(a => a.Index(index));
16+
var response = client.GetAlias(a => a.Index(index).RequestConfiguration(r=>r.ThrowExceptions()));
1717
return AliasesPointingToIndex(index, response);
1818
}
1919

@@ -23,7 +23,7 @@ public static IReadOnlyDictionary<string, AliasDefinition> GetAliasesPointingToI
2323
/// <param name="index">The index name we want to know aliases of</param>
2424
public static async Task<IReadOnlyDictionary<string, AliasDefinition>> GetAliasesPointingToIndexAsync(this IElasticClient client, IndexName index)
2525
{
26-
var response = await client.GetAliasAsync(a => a.Index(index)).ConfigureAwait(false);
26+
var response = await client.GetAliasAsync(a => a.Index(index).RequestConfiguration(r=>r.ThrowExceptions())).ConfigureAwait(false);
2727
return AliasesPointingToIndex(index, response);
2828
}
2929

src/Nest/Indices/AliasManagement/GetAlias/ElasticClient-GetIndicesPointingToAlias.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class IndicesPointingToAliasExtensions
1818
/// <param name="alias">The alias name(s)</param>
1919
public static IReadOnlyCollection<string> GetIndicesPointingToAlias(this IElasticClient client, Names alias)
2020
{
21-
var response = client.GetAlias(a => a.Name(alias));
21+
var response = client.GetAlias(a => a.Name(alias).RequestConfiguration(r=>r.ThrowExceptions()));
2222
return IndicesPointingToAlias(client.ConnectionSettings, alias, response);
2323
}
2424

@@ -29,7 +29,7 @@ public static IReadOnlyCollection<string> GetIndicesPointingToAlias(this IElasti
2929
/// <param name="alias">The alias name(s)</param>
3030
public static async Task<IReadOnlyCollection<string>> GetIndicesPointingToAliasAsync(this IElasticClient client, Names alias)
3131
{
32-
var response = await client.GetAliasAsync(a => a.Name(alias)).ConfigureAwait(false);
32+
var response = await client.GetAliasAsync(a => a.Name(alias).RequestConfiguration(r=>r.ThrowExceptions())).ConfigureAwait(false);
3333
return IndicesPointingToAlias(client.ConnectionSettings, alias, response);
3434
}
3535

0 commit comments

Comments
 (0)