Skip to content
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

feat: add Edge error handling #110

Merged
merged 3 commits into from
Aug 5, 2024
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

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

## 0.6.0 [2024-04-16]

Expand Down
59 changes: 58 additions & 1 deletion Client.Test/Internal/RestClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void ErrorBody()
}

[Test]
public void ErrorJsonBody()
public void ErrorJsonBodyCloud()
{
CreateAndConfigureRestClient(new ClientConfig
{
Expand All @@ -168,6 +168,63 @@ public void ErrorJsonBody()
});
}

[Test]
public void ErrorJsonBodyEdgeWithData()
{
CreateAndConfigureRestClient(new ClientConfig
{
Host = MockServerUrl,
});

MockServer
.Given(Request.Create().WithPath("/api").UsingPost())
.RespondWith(Response.Create()
.WithHeader("X-Influx-Error", "not used")
.WithBody("{\"error\":\"parsing failed\", \"data\":{\"error_message\":\"invalid field value in line protocol for field 'value' on line 0\"}}")
.WithStatusCode(401));

var ae = Assert.ThrowsAsync<InfluxDBApiException>(async () =>
{
await _client.Request("api", HttpMethod.Post);
});

Assert.Multiple(() =>
{
Assert.That(ae, Is.Not.Null);
Assert.That(ae.HttpResponseMessage, Is.Not.Null);
Assert.That(ae.Message, Is.EqualTo("invalid field value in line protocol for field 'value' on line 0"));
});
}

[Test]
public void ErrorJsonBodyEdgeWithoutData()
{
CreateAndConfigureRestClient(new ClientConfig
{
Host = MockServerUrl,
});

MockServer
.Given(Request.Create().WithPath("/api").UsingPost())
.RespondWith(Response.Create()
.WithHeader("X-Influx-Error", "not used")
.WithBody("{\"error\":\"token does not have sufficient permissions\"}")
.WithStatusCode(401));

var ae = Assert.ThrowsAsync<InfluxDBApiException>(async () =>
{
await _client.Request("api", HttpMethod.Post);
});

Assert.Multiple(() =>
{
Assert.That(ae, Is.Not.Null);
Assert.That(ae.HttpResponseMessage, Is.Not.Null);
Assert.That(ae.Message, Is.EqualTo("token does not have sufficient permissions"));
});
}


[Test]
public void ErrorReason()
{
Expand Down
29 changes: 27 additions & 2 deletions Client/Internal/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ internal async Task Request(string path, HttpMethod method, HttpContent? content
if (new DataContractJsonSerializer(typeof(ErrorBody)).ReadObject(memoryStream) is ErrorBody
errorBody)
{
message = errorBody.Message;
if (!string.IsNullOrEmpty(errorBody.Message)) // Cloud
{
message = errorBody.Message;
}
else if ((errorBody.Data is not null) && !string.IsNullOrEmpty(errorBody.Data.ErrorMessage)) // Edge
{
message = errorBody.Data.ErrorMessage;
}
else if (!string.IsNullOrEmpty(errorBody.Error)) // Edge
{
message = errorBody.Error;
}
}
}
catch (SerializationException se)
Expand Down Expand Up @@ -131,5 +142,19 @@ internal async Task Request(string path, HttpMethod method, HttpContent? content
[DataContract]
internal class ErrorBody
{
[DataMember(Name = "message")] public string? Message { get; set; }
[DataMember(Name = "message")]
public string? Message { get; set; }

[DataMember(Name = "error")]
public string? Error { get; set; }

[DataMember(Name = "data")]
public ErrorData? Data { get; set; }

[DataContract]
internal class ErrorData
{
[DataMember(Name = "error_message")]
public string? ErrorMessage { get; set; }
}
}
Loading