Skip to content

Commit 11a2ab3

Browse files
authored
Handle cat API 404 responses (#3944)
This commit adds handling for 404 responses for cat APIs. When a 404 is returned, NEST's HttpStatusCodeClassifier deems it to be successful, which the CatResponseBuilder tries to then deserialize the response to a collection of TCatRecord. When a 404 is returned however, the response is a JSON object with an error and status. The CatResponse<TRecord> should be deserialized from this when a 404 is returned. Fixes #3936
1 parent c4ed49d commit 11a2ab3

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/Nest/Cat/CatResponse.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Nest
88
public class CatResponse<TCatRecord> : ResponseBase
99
where TCatRecord : ICatRecord
1010
{
11+
[IgnoreDataMember]
1112
public IReadOnlyCollection<TCatRecord> Records { get; internal set; } = EmptyReadOnly<TCatRecord>.Collection;
1213
}
1314
}

src/Nest/Cat/CatResponseBuilder.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@ internal class CatResponseBuilder<TCatRecord> : CustomResponseBuilderBase where
1212

1313
public override object DeserializeResponse(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream)
1414
{
15-
var catResponse = new CatResponse<TCatRecord>();
16-
1715
if (!response.Success)
18-
return catResponse;
16+
return new CatResponse<TCatRecord>();
1917

18+
if (response.HttpStatusCode == 404)
19+
return builtInSerializer.Deserialize<CatResponse<TCatRecord>>(stream);
20+
21+
var catResponse = new CatResponse<TCatRecord>();
2022
var records = builtInSerializer.Deserialize<IReadOnlyCollection<TCatRecord>>(stream);
2123
catResponse.Records = records;
2224
return catResponse;
2325
}
2426

2527
public override async Task<object> DeserializeResponseAsync(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream, CancellationToken ctx = default)
2628
{
27-
var catResponse = new CatResponse<TCatRecord>();
28-
2929
if (!response.Success)
30-
return catResponse;
30+
return new CatResponse<TCatRecord>();
3131

32+
if (response.HttpStatusCode == 404)
33+
return await builtInSerializer.DeserializeAsync<CatResponse<TCatRecord>>(stream, ctx).ConfigureAwait(false);
34+
35+
var catResponse = new CatResponse<TCatRecord>();
3236
var records = await builtInSerializer.DeserializeAsync<IReadOnlyCollection<TCatRecord>>(stream, ctx).ConfigureAwait(false);
3337
catResponse.Records = records;
3438
return catResponse;

src/Tests/Tests/Cat/CatCount/CatCountApiTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,29 @@ protected override LazyResponses ClientUsage() => Calls(
4949
protected override void ExpectResponse(CatResponse<CatCountRecord> response) =>
5050
response.Records.Should().NotBeEmpty().And.Contain(a => a.Count != "0" && !string.IsNullOrEmpty(a.Count));
5151
}
52+
53+
public class CatCountNonExistentIndexApiTests
54+
: ApiIntegrationTestBase<ReadOnlyCluster, CatResponse<CatCountRecord>, ICatCountRequest, CatCountDescriptor, CatCountRequest>
55+
{
56+
public CatCountNonExistentIndexApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
57+
58+
protected override bool ExpectIsValid => false;
59+
protected override int ExpectStatusCode => 404;
60+
protected override HttpMethod HttpMethod => HttpMethod.GET;
61+
protected override string UrlPath => "/_cat/count/non-existent-index";
62+
63+
protected override LazyResponses ClientUsage() => Calls(
64+
(client, f) => client.Cat.Count(c => c.Index("non-existent-index")),
65+
(client, f) => client.Cat.CountAsync(c => c.Index("non-existent-index")),
66+
(client, r) => client.Cat.Count(new CatCountRequest("non-existent-index")),
67+
(client, r) => client.Cat.CountAsync(new CatCountRequest("non-existent-index"))
68+
);
69+
70+
protected override void ExpectResponse(CatResponse<CatCountRecord> response)
71+
{
72+
response.ApiCall.Success.Should().BeTrue();
73+
response.ServerError.Should().NotBeNull();
74+
response.Records.Should().NotBeNull().And.BeEmpty();
75+
}
76+
}
5277
}

0 commit comments

Comments
 (0)