Skip to content

Commit fd21d31

Browse files
authored
Merge pull request #2171 from elastic/fix/2.x-2152
Deserialize recursive error details
2 parents 602f0d2 + acfc949 commit fd21d31

File tree

6 files changed

+125
-6
lines changed

6 files changed

+125
-6
lines changed

src/Elasticsearch.Net/Responses/ServerError.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,25 @@ public class CausedBy
112112
{
113113
public string Reason { get; set; }
114114
public string Type { get; set; }
115+
public CausedBy InnerCausedBy { get; set; }
115116

116117
internal static CausedBy Create(IDictionary<string, object> dict, IJsonSerializerStrategy strategy)
117118
{
118119
var causedBy = new CausedBy();
119120
object reason;
120121
if (dict.TryGetValue("reason", out reason)) causedBy.Reason = Convert.ToString(reason);
121122
object type;
122-
if (dict.TryGetValue("type", out type)) causedBy.Type = Convert.ToString(type);
123+
if (dict.TryGetValue("type", out type)) causedBy.Type = Convert.ToString(type);
124+
object innerCausedBy;
125+
if (dict.TryGetValue("caused_by", out innerCausedBy))
126+
causedBy.InnerCausedBy = (CausedBy)strategy.DeserializeObject(innerCausedBy, typeof(CausedBy));
127+
123128
return causedBy;
124129
}
125130

126-
public override string ToString() => $"Type: {this.Type} Reason: \"{this.Reason}\"";
131+
public override string ToString() => this.InnerCausedBy == null
132+
? $"Type: {this.Type} Reason: \"{this.Reason}\""
133+
: $"Type: {this.Type} Reason: \"{this.Reason}\" CausedBy: \"{this.InnerCausedBy}\"";
127134
}
128135

129136
public class RootCause : IRootCause

src/Nest/CommonOptions/Failures/BulkError.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class BulkError
1717
[JsonProperty("reason")]
1818
public string Reason { get; internal set; }
1919

20-
public override string ToString() => $"Type: {Type} Reason: \"{Reason}\"";
20+
[JsonProperty("caused_by")]
21+
public CausedBy CausedBy { get; internal set; }
22+
23+
public override string ToString() => $"Type: {Type} Reason: \"{Reason}\" CausedBy: \"{CausedBy}\"";
2124
}
22-
}
25+
}

src/Nest/CommonOptions/Failures/ShardFailure.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@ public class CausedBy : IFailureReason
4242
{
4343
public string Type { get; internal set; }
4444
public string Reason { get; internal set; }
45+
46+
[JsonProperty("caused_by")]
47+
public CausedBy InnerCausedBy { get; internal set; }
4548
}
46-
}
49+
}

src/Tests/QueryDsl/Joining/HasChild/HasChildQueryUsageTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using Nest;
1+
using System;
2+
using System.Threading.Tasks;
3+
using Nest;
24
using Tests.Framework.Integration;
35
using Tests.Framework.MockData;
46

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Text;
5+
using Elasticsearch.Net;
6+
using FluentAssertions;
7+
using Nest;
8+
using Tests.Framework;
9+
using Tests.Framework.Integration;
10+
using Tests.Framework.MockData;
11+
12+
namespace Tests.Reproduce
13+
{
14+
public class GithubIssue2152
15+
{
16+
[U]
17+
public void CanDeserializeNestedBulkError()
18+
{
19+
var nestedCausedByError = @"{
20+
""took"": 4,
21+
""errors"": true,
22+
""items"": [{
23+
""update"": {
24+
""_index"": ""index-name"",
25+
""_type"": ""type-name"",
26+
""_id"": ""1"",
27+
""status"": 400,
28+
""error"": {
29+
""type"": ""illegal_argument_exception"",
30+
""reason"": ""failed to execute script"",
31+
""caused_by"": {
32+
""type"": ""script_exception"",
33+
""reason"": ""failed to run inline script [use(java.lang.Exception) {throw new Exception(\""Customized Exception\"")}] using lang [groovy]"",
34+
""caused_by"": {
35+
""type"": ""privileged_action_exception"",
36+
""reason"": null,
37+
""caused_by"": {
38+
""type"": ""exception"",
39+
""reason"": ""Custom Exception""
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}]
46+
}";
47+
48+
var bytes = Encoding.UTF8.GetBytes(nestedCausedByError);
49+
var connection = new InMemoryConnection(bytes);
50+
var settings = new ConnectionSettings(new SingleNodeConnectionPool(new Uri("http://localhost:9200")), connection);
51+
var client = new ElasticClient(settings);
52+
53+
var bulkResponse = client.Bulk(new BulkDescriptor());
54+
55+
bulkResponse.Errors.Should().BeTrue();
56+
57+
var firstOperation = bulkResponse.ItemsWithErrors.First();
58+
59+
firstOperation.Error.Should().NotBeNull();
60+
firstOperation.Error.CausedBy.Should().NotBeNull();
61+
firstOperation.Error.CausedBy.InnerCausedBy.Should().NotBeNull();
62+
firstOperation.Error.CausedBy.InnerCausedBy.InnerCausedBy.Should().NotBeNull();
63+
}
64+
65+
[U]
66+
public void CanDeserializeNestedError()
67+
{
68+
var nestedCausedByError = @"{
69+
""status"": 400,
70+
""error"": {
71+
""type"": ""illegal_argument_exception"",
72+
""reason"": ""failed to execute script"",
73+
""caused_by"": {
74+
""type"": ""script_exception"",
75+
""reason"": ""failed to run inline script [use(java.lang.Exception) {throw new Exception(\""Customized Exception\"")}] using lang [groovy]"",
76+
""caused_by"": {
77+
""type"": ""privileged_action_exception"",
78+
""reason"": null,
79+
""caused_by"": {
80+
""type"": ""exception"",
81+
""reason"": ""Custom Exception""
82+
}
83+
}
84+
}
85+
}
86+
}";
87+
88+
var bytes = Encoding.UTF8.GetBytes(nestedCausedByError);
89+
var connection = new InMemoryConnection(bytes, 400);
90+
var settings = new ConnectionSettings(new SingleNodeConnectionPool(new Uri("http://localhost:9200")), connection);
91+
var client = new ElasticClient(settings);
92+
93+
var searchResponse = client.Search<object>(s => s);
94+
95+
searchResponse.IsValid.Should().BeFalse();
96+
searchResponse.ServerError.Should().NotBeNull();
97+
searchResponse.ServerError.Error.Should().NotBeNull();
98+
searchResponse.ServerError.Error.CausedBy.Should().NotBeNull();
99+
searchResponse.ServerError.Error.CausedBy.InnerCausedBy.Should().NotBeNull();
100+
searchResponse.ServerError.Error.CausedBy.InnerCausedBy.InnerCausedBy.Should().NotBeNull();
101+
}
102+
}
103+
}

src/Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@
568568
<Compile Include="Framework\Integration\Process\ElasticsearchVersionInfo.cs" />
569569
<Compile Include="QueryDsl\Verbatim\VerbatimAndStrictQueryUsageTests.cs" />
570570
<Compile Include="Reproduce\GithubIssue2101.cs" />
571+
<Compile Include="Reproduce\GithubIssue2152.cs" />
571572
<Compile Include="Reproduce\GithubIssue2052.cs" />
572573
<Compile Include="Search\Search\Rescoring\RescoreUsageTests.cs" />
573574
<Compile Include="XPack\Security\ClearCachedRealms\ClearCachedRealmsApiTests.cs" />

0 commit comments

Comments
 (0)