Skip to content

Handle cat API 404 responses #3944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Nest/Cat/CatResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Nest
public class CatResponse<TCatRecord> : ResponseBase
where TCatRecord : ICatRecord
{
[IgnoreDataMember]
public IReadOnlyCollection<TCatRecord> Records { get; internal set; } = EmptyReadOnly<TCatRecord>.Collection;
}
}
16 changes: 10 additions & 6 deletions src/Nest/Cat/CatResponseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ internal class CatResponseBuilder<TCatRecord> : CustomResponseBuilderBase where

public override object DeserializeResponse(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream)
{
var catResponse = new CatResponse<TCatRecord>();

if (!response.Success)
return catResponse;
return new CatResponse<TCatRecord>();

if (response.HttpStatusCode == 404)
return builtInSerializer.Deserialize<CatResponse<TCatRecord>>(stream);

var catResponse = new CatResponse<TCatRecord>();
var records = builtInSerializer.Deserialize<IReadOnlyCollection<TCatRecord>>(stream);
catResponse.Records = records;
return catResponse;
}

public override async Task<object> DeserializeResponseAsync(IElasticsearchSerializer builtInSerializer, IApiCallDetails response, Stream stream, CancellationToken ctx = default)
{
var catResponse = new CatResponse<TCatRecord>();

if (!response.Success)
return catResponse;
return new CatResponse<TCatRecord>();

if (response.HttpStatusCode == 404)
return await builtInSerializer.DeserializeAsync<CatResponse<TCatRecord>>(stream, ctx).ConfigureAwait(false);

var catResponse = new CatResponse<TCatRecord>();
var records = await builtInSerializer.DeserializeAsync<IReadOnlyCollection<TCatRecord>>(stream, ctx).ConfigureAwait(false);
catResponse.Records = records;
return catResponse;
Expand Down
25 changes: 25 additions & 0 deletions src/Tests/Tests/Cat/CatCount/CatCountApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,29 @@ protected override LazyResponses ClientUsage() => Calls(
protected override void ExpectResponse(CatResponse<CatCountRecord> response) =>
response.Records.Should().NotBeEmpty().And.Contain(a => a.Count != "0" && !string.IsNullOrEmpty(a.Count));
}

public class CatCountNonExistentIndexApiTests
: ApiIntegrationTestBase<ReadOnlyCluster, CatResponse<CatCountRecord>, ICatCountRequest, CatCountDescriptor, CatCountRequest>
{
public CatCountNonExistentIndexApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override bool ExpectIsValid => false;
protected override int ExpectStatusCode => 404;
protected override HttpMethod HttpMethod => HttpMethod.GET;
protected override string UrlPath => "/_cat/count/non-existent-index";

protected override LazyResponses ClientUsage() => Calls(
(client, f) => client.Cat.Count(c => c.Index("non-existent-index")),
(client, f) => client.Cat.CountAsync(c => c.Index("non-existent-index")),
(client, r) => client.Cat.Count(new CatCountRequest("non-existent-index")),
(client, r) => client.Cat.CountAsync(new CatCountRequest("non-existent-index"))
);

protected override void ExpectResponse(CatResponse<CatCountRecord> response)
{
response.ApiCall.Success.Should().BeTrue();
response.ServerError.Should().NotBeNull();
response.Records.Should().NotBeNull().And.BeEmpty();
}
}
}