Skip to content

Commit 2269651

Browse files
authored
Improve Exception Messages (#38)
1 parent f34e654 commit 2269651

File tree

9 files changed

+113
-19
lines changed

9 files changed

+113
-19
lines changed

src/Elastic.Ingest.Elasticsearch/ElasticsearchChannelBase.Bootstrap.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ protected bool PutIndexTemplate(BootstrapMethod bootstrapMethod, string name, st
108108
return bootstrapMethod == BootstrapMethod.Silent
109109
? false
110110
: throw new Exception(
111-
$"Failure to create index templates for {TemplateWildcard}: {putIndexTemplateResponse}");
111+
$"Failure to create index templates for {TemplateWildcard}: {putIndexTemplateResponse}",
112+
putIndexTemplateResponse.ApiCallDetails.OriginalException
113+
);
112114
}
113115

114116
/// <summary></summary>
@@ -122,7 +124,9 @@ protected async Task<bool> PutIndexTemplateAsync(BootstrapMethod bootstrapMethod
122124
return bootstrapMethod == BootstrapMethod.Silent
123125
? false
124126
: throw new Exception(
125-
$"Failure to create index templates for {TemplateWildcard}: {putIndexTemplateResponse}");
127+
$"Failure to create index templates for {TemplateWildcard}: {putIndexTemplateResponse}",
128+
putIndexTemplateResponse.ApiCallDetails.OriginalException
129+
);
126130
}
127131

128132
/// <summary></summary>
@@ -135,7 +139,9 @@ protected bool PutComponentTemplate(BootstrapMethod bootstrapMethod, string name
135139
return bootstrapMethod == BootstrapMethod.Silent
136140
? false
137141
: throw new Exception(
138-
$"Failure to create component template `${name}` for {TemplateWildcard}: {putComponentTemplate}");
142+
$"Failure to create component template `{name}` for {TemplateWildcard}: {putComponentTemplate}",
143+
putComponentTemplate.ApiCallDetails.OriginalException
144+
);
139145
}
140146

141147
/// <summary></summary>
@@ -149,7 +155,9 @@ protected async Task<bool> PutComponentTemplateAsync(BootstrapMethod bootstrapMe
149155
return bootstrapMethod == BootstrapMethod.Silent
150156
? false
151157
: throw new Exception(
152-
$"Failure to create component template `${name}` for {TemplateWildcard}: {putComponentTemplate}");
158+
$"Failure to create component template `{name}` for {TemplateWildcard}: {putComponentTemplate}",
159+
putComponentTemplate.ApiCallDetails.OriginalException
160+
);
153161
}
154162

155163
/// <summary>

src/Elastic.Ingest.Elasticsearch/ElasticsearchChannelBase.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Elastic.Ingest.Elasticsearch.Serialization;
1515
using Elastic.Ingest.Transport;
1616
using Elastic.Transport;
17+
using Elastic.Transport.Products.Elasticsearch;
1718
using static Elastic.Ingest.Elasticsearch.ElasticsearchChannelStatics;
1819

1920
namespace Elastic.Ingest.Elasticsearch;
@@ -94,11 +95,11 @@ protected override Task<BulkResponse> ExportAsync(HttpTransport transport, Array
9495
protected abstract BulkOperationHeader CreateBulkOperationHeader(TEvent @event);
9596

9697
/// <summary> </summary>
97-
protected class HeadIndexTemplateResponse : TransportResponse { }
98+
protected class HeadIndexTemplateResponse : ElasticsearchResponse { }
9899

99100
/// <summary> </summary>
100-
protected class PutIndexTemplateResponse : TransportResponse { }
101+
protected class PutIndexTemplateResponse : ElasticsearchResponse { }
101102

102103
/// <summary> </summary>
103-
protected class PutComponentTemplateResponse : TransportResponse { }
104+
protected class PutComponentTemplateResponse : ElasticsearchResponse { }
104105
}

src/Elastic.Ingest.Elasticsearch/Serialization/BulkResponse.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
using System.Collections.ObjectModel;
77
using System.Text.Json;
88
using System.Text.Json.Serialization;
9-
using Elastic.Transport;
109
using Elastic.Transport.Products.Elasticsearch;
1110

1211
namespace Elastic.Ingest.Elasticsearch.Serialization;
1312

14-
1513
/// <summary>Represents the _bulk response from Elasticsearch</summary>
16-
public class BulkResponse : TransportResponse
14+
public class BulkResponse : ElasticsearchResponse
1715
{
1816
/// <summary>
1917
/// Individual bulk response items information
@@ -35,9 +33,6 @@ public bool TryGetServerErrorReason(out string? reason)
3533
reason = Error?.Reason;
3634
return !string.IsNullOrWhiteSpace(reason);
3735
}
38-
39-
/// <inheritdoc cref="object.ToString"/>
40-
public override string ToString() => ApiCallDetails.DebugInformation;
4136
}
4237

4338
internal class ResponseItemsConverter : JsonConverter<IReadOnlyCollection<BulkResponseItem>>

src/Elastic.Ingest.Transport/Elastic.Ingest.Transport.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<ItemGroup>
1313
<ProjectReference Include="..\Elastic.Channels\Elastic.Channels.csproj" />
14-
<PackageReference Include="Elastic.Transport" Version="0.4.9" />
14+
<PackageReference Include="Elastic.Transport" Version="0.4.11" />
1515
<PackageReference Include="System.Threading.Channels" Version="4.7.1" />
1616
</ItemGroup>
1717

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
using System;
5+
using System.Linq;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Elastic.Channels;
9+
using Elastic.Ingest.Elasticsearch.DataStreams;
10+
using Elastic.Transport;
11+
using FluentAssertions;
12+
using Xunit;
13+
using Xunit.Abstractions;
14+
15+
namespace Elastic.Ingest.Elasticsearch.IntegrationTests;
16+
17+
public class BootstrapFailureTests : IntegrationTestBase<SecurityCluster>
18+
{
19+
public BootstrapFailureTests(SecurityCluster cluster, ITestOutputHelper output) : base(cluster, output)
20+
{
21+
}
22+
23+
[Fact]
24+
public async Task BootstrapSilentShouldReportError()
25+
{
26+
var targetDataStream = new DataStreamName("logs", "silent-failure");
27+
var slim = new CountdownEvent(1);
28+
var transport = new DefaultHttpTransport(new TransportConfiguration(Cluster.NodesUris().First()));
29+
var options = new DataStreamChannelOptions<TimeSeriesDocument>(transport)
30+
{
31+
DataStream = targetDataStream,
32+
BufferOptions = new BufferOptions { WaitHandle = slim, OutboundBufferMaxSize = 1 }
33+
};
34+
var channel = new DataStreamChannel<TimeSeriesDocument>(options);
35+
var bootstrapped = await channel.BootstrapElasticsearchAsync(BootstrapMethod.Silent, "7-days-default");
36+
bootstrapped.Should().BeFalse("Insufficient rights");
37+
}
38+
39+
[Fact]
40+
public async Task BootstrapFailureShouldReportError()
41+
{
42+
var targetDataStream = new DataStreamName("logs", "exception-failure");
43+
var slim = new CountdownEvent(1);
44+
var transport = new DefaultHttpTransport(new TransportConfiguration(Cluster.NodesUris().First()));
45+
var options = new DataStreamChannelOptions<TimeSeriesDocument>(transport)
46+
{
47+
DataStream = targetDataStream,
48+
BufferOptions = new BufferOptions { WaitHandle = slim, OutboundBufferMaxSize = 1 }
49+
};
50+
var channel = new DataStreamChannel<TimeSeriesDocument>(options);
51+
Exception caughtException = null;
52+
try
53+
{
54+
await channel.BootstrapElasticsearchAsync(BootstrapMethod.Failure, "7-days-default");
55+
}
56+
catch (Exception e)
57+
{
58+
caughtException = e;
59+
}
60+
caughtException.Should().NotBeNull();
61+
62+
caughtException.Message.Should().StartWith("Failure to create component template `logs-exception-failure-settings` for logs-exception-failure-*:");
63+
caughtException.Message.Should().Contain("Could not authenticate with the specified node. Try verifying your credentials or check your Shield configuration.");
64+
caughtException.Message.Should().Contain("Invalid Elasticsearch response built from a unsuccessful (401) low level call on PUT:");
65+
}
66+
}

tests/Elastic.Ingest.Elasticsearch.IntegrationTests/Elastic.Ingest.Elasticsearch.IntegrationTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.0.4" />
10-
<PackageReference Include="Elastic.Elasticsearch.Xunit" Version="0.3.5" />
10+
<PackageReference Include="Elastic.Elasticsearch.Xunit" Version="0.4.0" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

tests/Elastic.Ingest.Elasticsearch.IntegrationTests/IngestionCluster.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Linq;
66
using Elastic.Clients.Elasticsearch;
7+
using Elastic.Elasticsearch.Ephemeral;
78
using Elastic.Elasticsearch.Xunit;
89
using Elastic.Transport;
910
using Xunit;
@@ -16,7 +17,9 @@ namespace Elastic.Ingest.Elasticsearch.IntegrationTests;
1617
/// <summary> Declare our cluster that we want to inject into our test classes </summary>
1718
public class IngestionCluster : XunitClusterBase
1819
{
19-
public IngestionCluster() : base(new XunitClusterConfiguration("8.7.0") { StartingPortNumber = 9202 }) { }
20+
protected static string Version = "8.7.0";
21+
public IngestionCluster() : this(new XunitClusterConfiguration(Version) { StartingPortNumber = 9202 }) { }
22+
public IngestionCluster(XunitClusterConfiguration xunitClusterConfiguration) : base(xunitClusterConfiguration) { }
2023

2124
public ElasticsearchClient CreateClient(ITestOutputHelper output) =>
2225
this.GetOrAddClient(_ =>
@@ -41,3 +44,13 @@ public ElasticsearchClient CreateClient(ITestOutputHelper output) =>
4144
return new ElasticsearchClient(settings);
4245
});
4346
}
47+
48+
public class SecurityCluster : IngestionCluster
49+
{
50+
public SecurityCluster() : base(new XunitClusterConfiguration(Version, ClusterFeatures.Security)
51+
{
52+
StartingPortNumber = 9202, TrialMode = XPackTrialMode.Trial
53+
,
54+
55+
}) { }
56+
}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
// Licensed to Elasticsearch B.V under one or more agreements.
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
4+
45
using Elastic.Clients.Elasticsearch;
56
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
67
using Xunit.Abstractions;
78

89
namespace Elastic.Ingest.Elasticsearch.IntegrationTests;
910

10-
public abstract class IntegrationTestBase : IClusterFixture<IngestionCluster>
11+
public abstract class IntegrationTestBase : IntegrationTestBase<IngestionCluster>
12+
{
13+
protected IntegrationTestBase(IngestionCluster cluster, ITestOutputHelper output) : base(cluster, output) { }
14+
}
15+
public abstract class IntegrationTestBase<TCluster> : IClusterFixture<TCluster>
16+
where TCluster : IngestionCluster, new()
1117
{
18+
protected IngestionCluster Cluster { get; }
1219
protected ElasticsearchClient Client { get; }
1320

14-
protected IntegrationTestBase(IngestionCluster cluster, ITestOutputHelper output) =>
21+
22+
protected IntegrationTestBase(IngestionCluster cluster, ITestOutputHelper output)
23+
{
24+
Cluster = cluster;
1525
Client = cluster.CreateClient(output);
26+
}
1627
}

tests/Elastic.Ingest.Elasticsearch.Tests/Elastic.Ingest.Elasticsearch.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Elastic.Transport.VirtualizedCluster" Version="0.4.5" />
8+
<PackageReference Include="Elastic.Transport.VirtualizedCluster" Version="0.4.10" />
99
</ItemGroup>
1010

1111
<ItemGroup>

0 commit comments

Comments
 (0)