Skip to content

Commit 6a3bb72

Browse files
authored
fix: decode and format error response from es (#205)
1 parent e554860 commit 6a3bb72

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

appender_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ func TestAppenderFlushRequestError(t *testing.T) {
687687
_, _, stat := docappendertest.DecodeBulkRequestWithStats(r)
688688
bytesUncompressedTotal += stat.UncompressedBytes
689689
w.WriteHeader(sc)
690+
w.Write([]byte(`{"error": {"root_cause": [{"type": "x_content_parse_exception","reason": "reason"}],"type": "x_content_parse_exception","reason": "reason","caused_by": {"type": "json_parse_exception","reason": "reason"}},"status": 400}`))
690691
})
691692
indexer, err := docappender.New(client, docappender.Config{FlushInterval: time.Minute})
692693
require.NoError(t, err)
@@ -722,22 +723,22 @@ func TestAppenderFlushRequestError(t *testing.T) {
722723
assert.Equal(t, wantStats, stats)
723724
}
724725
t.Run("400", func(t *testing.T) {
725-
test(t, http.StatusBadRequest, "flush failed (400): [400 Bad Request] ")
726+
test(t, http.StatusBadRequest, "flush failed (400): {\"error\":{\"type\":\"x_content_parse_exception\",\"caused_by\":{\"type\":\"json_parse_exception\"}}}")
726727
})
727728
t.Run("403", func(t *testing.T) {
728-
test(t, http.StatusForbidden, "flush failed (403): [403 Forbidden] ")
729+
test(t, http.StatusForbidden, "flush failed (403): {\"error\":{\"type\":\"x_content_parse_exception\",\"caused_by\":{\"type\":\"json_parse_exception\"}}}")
729730
})
730731
t.Run("429", func(t *testing.T) {
731-
test(t, http.StatusTooManyRequests, "flush failed (429): [429 Too Many Requests] ")
732+
test(t, http.StatusTooManyRequests, "flush failed (429): {\"error\":{\"type\":\"x_content_parse_exception\",\"caused_by\":{\"type\":\"json_parse_exception\"}}}")
732733
})
733734
t.Run("500", func(t *testing.T) {
734-
test(t, http.StatusInternalServerError, "flush failed (500): [500 Internal Server Error] ")
735+
test(t, http.StatusInternalServerError, "flush failed (500): {\"error\":{\"type\":\"x_content_parse_exception\",\"caused_by\":{\"type\":\"json_parse_exception\"}}}")
735736
})
736737
t.Run("503", func(t *testing.T) {
737-
test(t, http.StatusServiceUnavailable, "flush failed (503): [503 Service Unavailable] ")
738+
test(t, http.StatusServiceUnavailable, "flush failed (503): {\"error\":{\"type\":\"x_content_parse_exception\",\"caused_by\":{\"type\":\"json_parse_exception\"}}}")
738739
})
739740
t.Run("504", func(t *testing.T) {
740-
test(t, http.StatusGatewayTimeout, "flush failed (504): [504 Gateway Timeout] ")
741+
test(t, http.StatusGatewayTimeout, "flush failed (504): {\"error\":{\"type\":\"x_content_parse_exception\",\"caused_by\":{\"type\":\"json_parse_exception\"}}}")
741742
})
742743
}
743744

bulk_indexer.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package docappender
2020
import (
2121
"bytes"
2222
"context"
23+
"encoding/json"
2324
"errors"
2425
"fmt"
2526
"io"
@@ -389,7 +390,27 @@ func (b *BulkIndexer) Flush(ctx context.Context) (BulkIndexerResponseStat, error
389390
b.bytesUncompFlushed = bytesUncompFlushed
390391
var resp BulkIndexerResponseStat
391392
if res.IsError() {
392-
e := errorFlushFailed{resp: res.String(), statusCode: res.StatusCode}
393+
var s string
394+
var er struct {
395+
Error struct {
396+
Type string `json:"type,omitempty"`
397+
Reason string `json:"reason,omitempty"`
398+
CausedBy struct {
399+
Type string `json:"type,omitempty"`
400+
Reason string `json:"reason,omitempty"`
401+
} `json:"caused_by,omitempty"`
402+
} `json:"error,omitempty"`
403+
}
404+
405+
if err := jsoniter.NewDecoder(res.Body).Decode(&er); err == nil {
406+
er.Error.Reason = ""
407+
er.Error.CausedBy.Reason = ""
408+
409+
b, _ := json.Marshal(&er)
410+
s = string(b)
411+
}
412+
413+
e := errorFlushFailed{resp: s, statusCode: res.StatusCode}
393414
switch {
394415
case res.StatusCode == 429:
395416
e.tooMany = true

0 commit comments

Comments
 (0)