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

fix: InfluxDB Edge/OSS error handling #89

Merged
merged 5 commits into from
Jul 11, 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. [#87](https://github.com/InfluxCommunity/influxdb3-go/pull/87): Add Cloud Dedicated database creation support
1. [#89](https://github.com/InfluxCommunity/influxdb3-go/pull/89): InfluxDB Edge (OSS) error handling
1. [#91](https://github.com/InfluxCommunity/influxdb3-go/pull/91): Add Edge (OSS) authentication support.

## 0.8.0 [2024-06-24]
Expand Down
15 changes: 11 additions & 4 deletions influxdb3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,11 @@ func (c *Client) resolveHTTPError(r *http.Response) error {

var httpError struct {
ServerError
// Error message of InfluxDB 1 error
// InfluxDB Edge/OSS error message fields
Error string `json:"error"`
Data struct {
ErrorMessage string `json:"error_message"`
} `json:"data"`
}

httpError.StatusCode = r.StatusCode
Expand All @@ -217,13 +220,17 @@ func (c *Client) resolveHTTPError(r *http.Response) error {
httpError.Message = fmt.Sprintf("cannot read error response:: %v", err)
}
ctype, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
if ctype == "application/json" {
if ctype == "application/json" || ctype == "" {
err := json.Unmarshal(body, &httpError)
if err != nil {
if err != nil && ctype != "" {
httpError.Message = fmt.Sprintf("cannot decode error response: %v", err)
}
if httpError.Message == "" && httpError.Code == "" {
httpError.Message = httpError.Error
if len(httpError.Data.ErrorMessage) > 0 {
httpError.Message = httpError.Data.ErrorMessage
} else {
httpError.Message = httpError.Error
}
}
}
if httpError.Message == "" {
Expand Down
27 changes: 26 additions & 1 deletion influxdb3/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func TestResolveErrorWrongJsonResponse(t *testing.T) {
assert.Equal(t, "cannot decode error response: unexpected end of JSON input", err.Error())
}

func TestResolveErrorV1(t *testing.T) {
func TestResolveErrorEdge(t *testing.T) {
errMsg := "compilation failed: error at @1:170-1:171: invalid expression @1:167-1:168: |"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
Expand All @@ -513,6 +513,31 @@ func TestResolveErrorV1(t *testing.T) {
assert.Equal(t, errMsg, err.Error())
}

func TestResolveErrorEdgeWithData(t *testing.T) {
errMsg := "compilation failed"
dataErrMsg := "compilation failed: error at @1:170-1:171: invalid expression @1:167-1:168: |"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(400)
_, _ = w.Write([]byte(`{"error": "` + errMsg + `", "data": {"error_message": "` + dataErrMsg + `"}}`))
}))
defer ts.Close()
client, err := New(ClientConfig{Host: ts.URL, Token: "my-token"})
require.NoError(t, err)
turl, err := url.Parse(ts.URL)
require.NoError(t, err)
res, err := client.makeAPICall(context.Background(), httpParams{
endpointURL: turl,
queryParams: nil,
httpMethod: "GET",
headers: nil,
body: nil,
})
assert.Nil(t, res)
require.Error(t, err)
assert.Equal(t, dataErrMsg, err.Error())
}

func TestResolveErrorNoError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
Expand Down