Skip to content

Commit c0e42c8

Browse files
authored
feat: add Edge error handling (#110)
1 parent eaf571a commit c0e42c8

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
1. [#101](https://github.com/InfluxCommunity/influxdb3-csharp/pull/101): Add standard `user-agent` header to all calls.
6+
1. [#110](https://github.com/InfluxCommunity/influxdb3-csharp/pull/110): InfluxDB Edge (OSS) error handling.
67

78
## 0.6.0 [2024-04-16]
89

Client.Test/Internal/RestClientTest.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void ErrorBody()
141141
}
142142

143143
[Test]
144-
public void ErrorJsonBody()
144+
public void ErrorJsonBodyCloud()
145145
{
146146
CreateAndConfigureRestClient(new ClientConfig
147147
{
@@ -168,6 +168,63 @@ public void ErrorJsonBody()
168168
});
169169
}
170170

171+
[Test]
172+
public void ErrorJsonBodyEdgeWithData()
173+
{
174+
CreateAndConfigureRestClient(new ClientConfig
175+
{
176+
Host = MockServerUrl,
177+
});
178+
179+
MockServer
180+
.Given(Request.Create().WithPath("/api").UsingPost())
181+
.RespondWith(Response.Create()
182+
.WithHeader("X-Influx-Error", "not used")
183+
.WithBody("{\"error\":\"parsing failed\", \"data\":{\"error_message\":\"invalid field value in line protocol for field 'value' on line 0\"}}")
184+
.WithStatusCode(401));
185+
186+
var ae = Assert.ThrowsAsync<InfluxDBApiException>(async () =>
187+
{
188+
await _client.Request("api", HttpMethod.Post);
189+
});
190+
191+
Assert.Multiple(() =>
192+
{
193+
Assert.That(ae, Is.Not.Null);
194+
Assert.That(ae.HttpResponseMessage, Is.Not.Null);
195+
Assert.That(ae.Message, Is.EqualTo("invalid field value in line protocol for field 'value' on line 0"));
196+
});
197+
}
198+
199+
[Test]
200+
public void ErrorJsonBodyEdgeWithoutData()
201+
{
202+
CreateAndConfigureRestClient(new ClientConfig
203+
{
204+
Host = MockServerUrl,
205+
});
206+
207+
MockServer
208+
.Given(Request.Create().WithPath("/api").UsingPost())
209+
.RespondWith(Response.Create()
210+
.WithHeader("X-Influx-Error", "not used")
211+
.WithBody("{\"error\":\"token does not have sufficient permissions\"}")
212+
.WithStatusCode(401));
213+
214+
var ae = Assert.ThrowsAsync<InfluxDBApiException>(async () =>
215+
{
216+
await _client.Request("api", HttpMethod.Post);
217+
});
218+
219+
Assert.Multiple(() =>
220+
{
221+
Assert.That(ae, Is.Not.Null);
222+
Assert.That(ae.HttpResponseMessage, Is.Not.Null);
223+
Assert.That(ae.Message, Is.EqualTo("token does not have sufficient permissions"));
224+
});
225+
}
226+
227+
171228
[Test]
172229
public void ErrorReason()
173230
{

Client/Internal/RestClient.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,18 @@ internal async Task Request(string path, HttpMethod method, HttpContent? content
9393
if (new DataContractJsonSerializer(typeof(ErrorBody)).ReadObject(memoryStream) is ErrorBody
9494
errorBody)
9595
{
96-
message = errorBody.Message;
96+
if (!string.IsNullOrEmpty(errorBody.Message)) // Cloud
97+
{
98+
message = errorBody.Message;
99+
}
100+
else if ((errorBody.Data is not null) && !string.IsNullOrEmpty(errorBody.Data.ErrorMessage)) // Edge
101+
{
102+
message = errorBody.Data.ErrorMessage;
103+
}
104+
else if (!string.IsNullOrEmpty(errorBody.Error)) // Edge
105+
{
106+
message = errorBody.Error;
107+
}
97108
}
98109
}
99110
catch (SerializationException se)
@@ -131,5 +142,19 @@ internal async Task Request(string path, HttpMethod method, HttpContent? content
131142
[DataContract]
132143
internal class ErrorBody
133144
{
134-
[DataMember(Name = "message")] public string? Message { get; set; }
145+
[DataMember(Name = "message")]
146+
public string? Message { get; set; }
147+
148+
[DataMember(Name = "error")]
149+
public string? Error { get; set; }
150+
151+
[DataMember(Name = "data")]
152+
public ErrorData? Data { get; set; }
153+
154+
[DataContract]
155+
internal class ErrorData
156+
{
157+
[DataMember(Name = "error_message")]
158+
public string? ErrorMessage { get; set; }
159+
}
135160
}

0 commit comments

Comments
 (0)