Skip to content

Commit 73f6709

Browse files
Mpdreamzgmarz
authored andcommitted
error:, should be deserialized into ServerError as well (#2586)
1 parent 3b10358 commit 73f6709

File tree

3 files changed

+122
-44
lines changed

3 files changed

+122
-44
lines changed

src/Elasticsearch.Net/Responses/ServerError.cs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ public class ServerError
1212
{
1313
public Error Error { get; set; }
1414
public int Status { get; set; }
15-
16-
public static ServerError Create(Stream stream) => ElasticsearchDefaultSerializer.Instance.Deserialize<ServerError>(stream);
15+
16+
public static ServerError Create(Stream stream) => ElasticsearchDefaultSerializer.Instance.Deserialize<ServerError>(stream);
1717
public static Task<ServerError> CreateAsync(Stream stream, CancellationToken token) =>
1818
ElasticsearchDefaultSerializer.Instance.DeserializeAsync<ServerError>(stream, token);
19-
20-
/// <summary>
21-
/// Creating the server error might fail in cases where a proxy returns an http response which is not json at all
22-
/// </summary>
23-
public static bool TryCreate(Stream stream, out ServerError serverError)
24-
{
19+
20+
/// <summary>
21+
/// Creating the server error might fail in cases where a proxy returns an http response which is not json at all
22+
/// </summary>
23+
public static bool TryCreate(Stream stream, out ServerError serverError)
24+
{
2525
serverError = null;
26-
try { serverError = Create(stream); }
27-
catch { return false; }
28-
return true;
26+
try { serverError = Create(stream); }
27+
catch { return false; }
28+
return true;
2929
}
30-
31-
/// <summary>
32-
/// Creating the server error might fail in cases where a proxy returns an http response which is not json at all
33-
/// </summary>
34-
public static async Task<ServerError> TryCreateAsync(Stream stream, CancellationToken token)
35-
{
30+
31+
/// <summary>
32+
/// Creating the server error might fail in cases where a proxy returns an http response which is not json at all
33+
/// </summary>
34+
public static async Task<ServerError> TryCreateAsync(Stream stream, CancellationToken token)
35+
{
3636
try { return await CreateAsync(stream, token).ConfigureAwait(false); }
3737
catch { // ignored
3838
}
@@ -48,11 +48,18 @@ internal static ServerError Create(IDictionary<string, object> dict, IJsonSerial
4848
statusCode = Convert.ToInt32(status);
4949

5050
if (!dict.TryGetValue("error", out error)) return null;
51+
Error err;
52+
var s = error as string;
53+
if (s != null)
54+
{
55+
err = new Error {Reason = s};
56+
}
57+
else err = (Error) strategy.DeserializeObject(error, typeof(Error));
5158

5259
return new ServerError
5360
{
5461
Status = statusCode,
55-
Error = (Error)strategy.DeserializeObject(error, typeof(Error))
62+
Error = err
5663
};
5764
}
5865

@@ -88,8 +95,8 @@ public class Error : IRootCause
8895
internal static Error Create(IDictionary<string, object> dict, IJsonSerializerStrategy strategy)
8996
{
9097
var error = new Error();
91-
error.FillValues(dict);
92-
98+
error.FillValues(dict);
99+
93100
object causedBy;
94101
if (dict.TryGetValue("caused_by", out causedBy))
95102
error.CausedBy = (CausedBy) strategy.DeserializeObject(causedBy, typeof(CausedBy));
@@ -116,12 +123,12 @@ public class CausedBy
116123

117124
internal static CausedBy Create(IDictionary<string, object> dict, IJsonSerializerStrategy strategy)
118125
{
119-
var causedBy = new CausedBy();
120-
object reason;
121-
if (dict.TryGetValue("reason", out reason)) causedBy.Reason = Convert.ToString(reason);
122-
object type;
123-
if (dict.TryGetValue("type", out type)) causedBy.Type = Convert.ToString(type);
124-
object innerCausedBy;
126+
var causedBy = new CausedBy();
127+
object reason;
128+
if (dict.TryGetValue("reason", out reason)) causedBy.Reason = Convert.ToString(reason);
129+
object type;
130+
if (dict.TryGetValue("type", out type)) causedBy.Type = Convert.ToString(type);
131+
object innerCausedBy;
125132
if (dict.TryGetValue("caused_by", out innerCausedBy))
126133
causedBy.InnerCausedBy = (CausedBy)strategy.DeserializeObject(innerCausedBy, typeof(CausedBy));
127134

@@ -146,24 +153,25 @@ internal static RootCause Create(IDictionary<string, object> dict, IJsonSerializ
146153
var rootCause = new RootCause();
147154
rootCause.FillValues(dict);
148155
return rootCause;
149-
}
150-
156+
}
157+
151158
public override string ToString() => $"Type: {this.Type} Reason: \"{this.Reason}\"";
152159
}
153160

154161
internal static class RootCauseExtensions
155162
{
156163
public static void FillValues(this IRootCause rootCause, IDictionary<string, object> dict)
157164
{
165+
if (dict == null) return;
158166
object index;
159-
if (dict.TryGetValue("index", out index)) rootCause.Index = Convert.ToString(index);
160-
object reason;
161-
if (dict.TryGetValue("reason", out reason)) rootCause.Reason = Convert.ToString(reason);
162-
object resourceId;
167+
if (dict.TryGetValue("index", out index)) rootCause.Index = Convert.ToString(index);
168+
object reason;
169+
if (dict.TryGetValue("reason", out reason)) rootCause.Reason = Convert.ToString(reason);
170+
object resourceId;
163171
if (dict.TryGetValue("resource.id", out resourceId)) rootCause.ResourceId = Convert.ToString(resourceId);
164-
object resourceType;
165-
if (dict.TryGetValue("resource.type", out resourceType)) rootCause.ResourceType = Convert.ToString(resourceType);
166-
object type;
172+
object resourceType;
173+
if (dict.TryGetValue("resource.type", out resourceType)) rootCause.ResourceType = Convert.ToString(resourceType);
174+
object type;
167175
if (dict.TryGetValue("type", out type)) rootCause.Type = Convert.ToString(type);
168176
}
169177
}

src/Tests/Indices/AliasManagement/AliasExists/AliasExistsApiTests.cs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using Elasticsearch.Net;
4+
using FluentAssertions;
45
using Nest;
56
using Tests.Framework;
67
using Tests.Framework.Integration;
@@ -13,14 +14,16 @@ public class AliasExistsApiTests
1314
: ApiIntegrationTestBase<WritableCluster, IExistsResponse, IAliasExistsRequest, AliasExistsDescriptor, AliasExistsRequest>
1415

1516
{
16-
public AliasExistsApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
17-
18-
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
19-
{
20-
foreach (var index in values.Values)
21-
client.CreateIndex(index, c=>c
22-
.Aliases(aa=>aa.Alias(index + "-alias"))
23-
);
17+
public AliasExistsApiTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage)
18+
{
19+
}
20+
21+
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
22+
{
23+
foreach (var index in values.Values)
24+
client.CreateIndex(index, c => c
25+
.Aliases(aa => aa.Alias(index + "-alias"))
26+
);
2427
}
2528

2629
protected override LazyResponses ClientUsage() => Calls(
@@ -42,4 +45,38 @@ protected override LazyResponses ClientUsage() => Calls(
4245

4346
protected override AliasExistsRequest Initializer => new AliasExistsRequest(Names(CallIsolatedValue + "-alias"));
4447
}
48+
49+
public class AliasExistsNotFoundApiTests
50+
: ApiIntegrationTestBase<ReadOnlyCluster, IExistsResponse, IAliasExistsRequest, AliasExistsDescriptor, AliasExistsRequest>
51+
52+
{
53+
public AliasExistsNotFoundApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage)
54+
{
55+
}
56+
57+
protected override LazyResponses ClientUsage() => Calls(
58+
fluent: (client, f) => client.AliasExists(f),
59+
fluentAsync: (client, f) => client.AliasExistsAsync(f),
60+
request: (client, r) => client.AliasExists(r),
61+
requestAsync: (client, r) => client.AliasExistsAsync(r)
62+
);
63+
64+
protected override bool ExpectIsValid => true;
65+
protected override int ExpectStatusCode => 404;
66+
protected override HttpMethod HttpMethod => HttpMethod.HEAD;
67+
protected override string UrlPath => $"/_alias/unknown-alias";
68+
69+
protected override bool SupportsDeserialization => false;
70+
71+
protected override Func<AliasExistsDescriptor, IAliasExistsRequest> Fluent => d => d
72+
.Name("unknown-alias");
73+
74+
protected override AliasExistsRequest Initializer => new AliasExistsRequest(Names("unknown-alias"));
75+
76+
protected override void ExpectResponse(IExistsResponse response)
77+
{
78+
response.ServerError.Should().BeNull();
79+
response.Exists.Should().BeFalse();
80+
}
81+
}
4582
}

src/Tests/Indices/AliasManagement/GetAlias/GetAliasApiTests.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using Elasticsearch.Net;
45
using FluentAssertions;
56
using Nest;
@@ -33,9 +34,41 @@ protected override void ExpectResponse(IGetAliasResponse response)
3334
protected override bool SupportsDeserialization => false;
3435

3536
protected override Func<GetAliasDescriptor, IGetAliasRequest> Fluent => d=>d
36-
.AllIndices()
3737
.Name(Names)
3838
;
3939
protected override GetAliasRequest Initializer => new GetAliasRequest(Infer.AllIndices, Names);
4040
}
41+
42+
public class GetAliasNotFoundApiTests : ApiIntegrationTestBase<ReadOnlyCluster, IGetAliasResponse, IGetAliasRequest, GetAliasDescriptor, GetAliasRequest>
43+
{
44+
private static readonly Names Names = Infer.Names("bad-alias");
45+
46+
public GetAliasNotFoundApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
47+
48+
protected override LazyResponses ClientUsage() => Calls(
49+
fluent: (client, f) => client.GetAlias(f),
50+
fluentAsync: (client, f) => client.GetAliasAsync(f),
51+
request: (client, r) => client.GetAlias(r),
52+
requestAsync: (client, r) => client.GetAliasAsync(r)
53+
);
54+
55+
protected override bool ExpectIsValid => false;
56+
protected override int ExpectStatusCode => 200;
57+
protected override HttpMethod HttpMethod => HttpMethod.GET;
58+
protected override string UrlPath => $"/_all/_alias/alias%2Cx%2Cy";
59+
protected override bool SupportsDeserialization => false;
60+
61+
protected override Func<GetAliasDescriptor, IGetAliasRequest> Fluent => d=>d
62+
.Name(Names)
63+
;
64+
protected override GetAliasRequest Initializer => new GetAliasRequest(Names);
65+
66+
protected override void ExpectResponse(IGetAliasResponse response)
67+
{
68+
response.ServerError.Should().NotBeNull();
69+
response.ServerError.Error.Reason.Should().Contain("missing");
70+
response.Indices.Should().NotBeNull();
71+
}
72+
73+
}
4174
}

0 commit comments

Comments
 (0)