Skip to content

Commit 2a147c4

Browse files
committed
improved dispatch exception to indicate which values are missing
1 parent 307fe5d commit 2a147c4

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

Diff for: src/CodeGeneration/CodeGeneration.LowLevelClient/Views/_LowLevelDispatch.Generated.cshtml

-11
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ namespace Nest
1818
///<summary>This dispatches highlevel requests into the proper lowlevel client overload method</summary>
1919
internal partial class LowLevelDispatch
2020
{
21-
internal static ElasticsearchClientException InvalidDispatch(string apiCall, IRequest provided, HttpMethod[] methods, params string[] endpoints)
22-
{
23-
var sb = new StringBuilder();
24-
sb.AppendLine($"Dispatching {apiCall}() from NEST into to Elasticsearch.NET failed");
25-
sb.AppendLine($"Received a request marked as {provided.HttpMethod.GetStringValue()}");
26-
sb.AppendLine($"This endpoint accepts {string.Join(",", methods.Select(p=>p.GetStringValue()))}");
27-
sb.AppendLine($"The request might not have enough information provided to make any of these endpoints:");
28-
foreach (var endpoint in endpoints)
29-
sb.AppendLine($" - {endpoint}");
30-
return new ElasticsearchClientException(sb.ToString());
31-
}
3221

3322
@foreach (var kv in model.Endpoints)
3423
{

Diff for: src/Nest/CommonAbstractions/LowLevelDispatch/LowLevelDispatch.cs

+51-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
using System.Text.RegularExpressions;
25
using Elasticsearch.Net;
36

47
namespace Nest
@@ -12,8 +15,53 @@ public LowLevelDispatch(IElasticLowLevelClient rawElasticClient)
1215
this._lowLevel = rawElasticClient;
1316
}
1417

15-
internal bool AllSet(params string[] pathVariables) => pathVariables.All(p => !p.IsNullOrEmpty()) && !pathVariables.All(p=>p=="_all");
18+
internal bool AllSet(params string[] pathVariables) => pathVariables.All(p => !p.IsNullOrEmpty()) && !pathVariables.All(p => p == "_all");
19+
20+
internal bool AllSet(params IUrlParameter[] pathVariables) => pathVariables.All(p => p != null);
21+
22+
internal static Exception InvalidDispatch(string apiCall, IRequest provided, HttpMethod[] methods, params string[] endpoints)
23+
{
24+
var sb = new StringBuilder();
25+
sb.AppendLine($"Dispatching {apiCall}() from NEST into to Elasticsearch.NET failed");
26+
sb.AppendLine($"Received a request marked as {provided.HttpMethod.GetStringValue()}");
27+
sb.AppendLine($"This endpoint accepts {string.Join(",", methods.Select(p=>p.GetStringValue()))}");
28+
sb.AppendLine($"The request might not have enough information provided to make any of these endpoints:");
29+
foreach (var endpoint in endpoints)
30+
sb.AppendLine($" - {PrettyPrintEndpoint(provided, endpoint)}");
31+
32+
return new ArgumentException(sb.ToString());
33+
}
34+
35+
private static Regex ReplaceParams = new Regex(@"\{(.+?)\}");
36+
37+
internal static string PrettyPrintEndpoint(IRequest request, string endpoint)
38+
{
39+
var pretty = ReplaceParams.Replace(endpoint, (m) =>
40+
{
41+
var key = m.Groups[1].Value.ToLowerInvariant();
42+
switch(key)
43+
{
44+
case "index" : return PrettyParam(key, request.RouteValues.Index);
45+
case "name" : return PrettyParam(key, request.RouteValues.Name);
46+
case "feature" : return PrettyParam(key, request.RouteValues.Feature);
47+
case "field" : return PrettyParam(key, request.RouteValues.Field);
48+
case "fields" : return PrettyParam(key, request.RouteValues.Fields);
49+
case "id" : return PrettyParam(key, request.RouteValues.Id);
50+
case "index_metric" : return PrettyParam(key, request.RouteValues.IndexMetric);
51+
case "lang" : return PrettyParam(key, request.RouteValues.Lang);
52+
case "metric" : return PrettyParam(key, request.RouteValues.Metric);
53+
case "node_id" : return PrettyParam(key, request.RouteValues.NodeId);
54+
case "repository" : return PrettyParam(key, request.RouteValues.Repository);
55+
case "scroll_id" : return PrettyParam(key, request.RouteValues.ScrollId);
56+
case "snapshot" : return PrettyParam(key, request.RouteValues.Snapshot);
57+
case "type" : return PrettyParam(key, request.RouteValues.Type);
58+
default: return PrettyParam(key, "<Unknown route variable>");
59+
}
60+
});
61+
return pretty;
62+
}
63+
64+
private static string PrettyParam(string key, string value) => $"{{{key}={(value.IsNullOrEmpty() ? "<NULL>" : value)}}}";
1665

17-
internal bool AllSet(params IUrlParameter[] pathVariables) => pathVariables.All(p => p != null);
1866
}
1967
}

Diff for: src/Nest/_Generated/_LowLevelDispatch.generated.cs

-12
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ namespace Nest
1515
///<summary>This dispatches highlevel requests into the proper lowlevel client overload method</summary>
1616
internal partial class LowLevelDispatch
1717
{
18-
internal static ElasticsearchClientException InvalidDispatch(string apiCall, IRequest provided, HttpMethod[] methods, params string[] endpoints)
19-
{
20-
var sb = new StringBuilder();
21-
sb.AppendLine($"Dispatching {apiCall}() from NEST into to Elasticsearch.NET failed");
22-
sb.AppendLine($"Received a request marked as {provided.HttpMethod.GetStringValue()}");
23-
sb.AppendLine($"This endpoint accepts {string.Join(",", methods.Select(p=>p.GetStringValue()))}");
24-
sb.AppendLine($"The request might not have enough information provided to make any of these endpoints:");
25-
foreach (var endpoint in endpoints)
26-
sb.AppendLine($" - {endpoint}");
27-
return new ElasticsearchClientException(sb.ToString());
28-
}
29-
3018
internal ElasticsearchResponse<T> BulkDispatch<T>(IRequest<BulkRequestParameters> p , PostData<object> body) where T : class
3119
{
3220
switch(p.HttpMethod)

Diff for: src/Tests/ClientConcepts/Exceptions/ExceptionTests.cs

+12
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,17 @@ public void ClientTestWhenThrowExceptionsDisabled()
8383
#endif
8484
response.CallDetails.ServerError.Should().BeNull();
8585
}
86+
87+
[U]
88+
public void DispatchIndicatesMissingRouteValues()
89+
{
90+
var settings = new ConnectionSettings(new Uri("http://doesntexist:9200"));
91+
var client = new ElasticClient(settings);
92+
93+
Action dispatch = () => client.Index(new Project());
94+
var ce = dispatch.ShouldThrow<ArgumentException>();
95+
ce.Should().NotBeNull();
96+
ce.Which.Message.Should().Contain("index=<NULL>");
97+
}
8698
}
8799
}

0 commit comments

Comments
 (0)