Skip to content

Commit 82b30b5

Browse files
committed
Merge pull request #1877 from elastic/fix/server-error-caused-by
Deserialize caused_by as part of ServerError
2 parents 3ae4a21 + af252be commit 82b30b5

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/Elasticsearch.Net/Responses/ServerError.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ internal static ServerError Create(IDictionary<string, object> dict, IJsonSerial
4343
{
4444
object status, error;
4545
int statusCode = -1;
46+
4647
if (dict.TryGetValue("status", out status))
4748
statusCode = Convert.ToInt32(status);
4849

@@ -82,11 +83,17 @@ public class Error : IRootCause
8283
public string ResourceType { get; set; }
8384
public string Type { get; set; }
8485
public List<RootCause> RootCause { get; set; }
86+
public CausedBy CausedBy { get; set; }
8587

8688
internal static Error Create(IDictionary<string, object> dict, IJsonSerializerStrategy strategy)
8789
{
8890
var error = new Error();
89-
error.FillValues(dict);
91+
error.FillValues(dict);
92+
93+
object causedBy;
94+
if (dict.TryGetValue("caused_by", out causedBy))
95+
error.CausedBy = (CausedBy) strategy.DeserializeObject(causedBy, typeof(CausedBy));
96+
9097
object rootCause;
9198
if (!dict.TryGetValue("root_cause", out rootCause)) return error;
9299

@@ -96,6 +103,26 @@ internal static Error Create(IDictionary<string, object> dict, IJsonSerializerSt
96103
return error;
97104
}
98105

106+
public override string ToString() => CausedBy == null
107+
? $"Type: {this.Type} Reason: \"{this.Reason}\""
108+
: $"Type: {this.Type} Reason: \"{this.Reason}\" CausedBy: \"{this.CausedBy}\"";
109+
}
110+
111+
public class CausedBy
112+
{
113+
public string Reason { get; set; }
114+
public string Type { get; set; }
115+
116+
internal static CausedBy Create(IDictionary<string, object> dict, IJsonSerializerStrategy strategy)
117+
{
118+
var causedBy = new CausedBy();
119+
object reason;
120+
if (dict.TryGetValue("reason", out reason)) causedBy.Reason = Convert.ToString(reason);
121+
object type;
122+
if (dict.TryGetValue("type", out type)) causedBy.Type = Convert.ToString(type);
123+
return causedBy;
124+
}
125+
99126
public override string ToString() => $"Type: {this.Type} Reason: \"{this.Reason}\"";
100127
}
101128

src/Elasticsearch.Net/Serialization/ElasticsearchNetJsonStrategy.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public override object DeserializeObject(object value, Type type)
2929
var dict = base.DeserializeObject(value, typeof(IDictionary<string, object>)) as IDictionary<string, object>;
3030
return RootCause.Create(dict, this);
3131
}
32+
if (type == typeof(CausedBy))
33+
{
34+
var dict = base.DeserializeObject(value, typeof(IDictionary<string, object>)) as IDictionary<string, object>;
35+
return CausedBy.Create(dict, this);
36+
}
3237
return base.DeserializeObject(value, type);
3338
}
3439
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices.ComTypes;
5+
using System.Threading.Tasks;
6+
using Tests.Framework;
7+
using Elasticsearch.Net;
8+
using FluentAssertions;
9+
using Xunit;
10+
11+
namespace Tests.ClientConcepts.ServerError
12+
{
13+
public class ServerErrorTests : SerializationTestBase
14+
{
15+
[U]
16+
public void CanDeserializeServerError()
17+
{
18+
var serverErrorJson = @"{
19+
""error"": {
20+
""root_cause"": [
21+
{
22+
""type"": ""parse_exception"",
23+
""reason"": ""failed to parse source for create index""
24+
}
25+
],
26+
""type"": ""parse_exception"",
27+
""reason"": ""failed to parse source for create index"",
28+
""caused_by"": {
29+
""type"": ""json_parse_exception"",
30+
""reason"": ""Unexpected character ('\""' (code 34)): was expecting a colon to separate field name and value\n at [Source: [B@1231dcb3; line: 6, column: 10]""
31+
}
32+
},
33+
""status"": 400
34+
}";
35+
36+
var serverError = this.Deserialize<Elasticsearch.Net.ServerError>(serverErrorJson);
37+
38+
serverError.Should().NotBeNull();
39+
serverError.Status.Should().Be(400);
40+
41+
serverError.Error.Should().NotBeNull();
42+
serverError.Error.RootCause.Count.Should().Be(1);
43+
serverError.Error.CausedBy.Should().NotBeNull();
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)