-
Notifications
You must be signed in to change notification settings - Fork 82
/
error.go
174 lines (153 loc) · 4.21 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package es
import (
"encoding/json"
"errors"
"strconv"
"strings"
)
const (
unknownErrorType = "unknown_error"
timeoutErrorType = "timeout_exception"
indexNotFoundErrorType = "index_not_found_exception"
versionConflictErrorType = "version_conflict_engine_exception"
)
// TODO: Why do we have both ErrElastic and ErrorT? Very strange.
type ErrElastic struct {
Status int
Type string
Reason string
Cause struct {
Type string
Reason string
}
}
func (e *ErrElastic) Unwrap() error {
if e.Type == indexNotFoundErrorType {
return ErrIndexNotFound
} else if e.Type == timeoutErrorType {
return ErrTimeout
}
return nil
}
func (e ErrElastic) Error() string {
// Improved error string to account on missing empty e.Type and e.Reason
// Otherwise were getting: "elastic fail 404::"
msg := "elastic fail "
var b strings.Builder
b.Grow(len(msg) + 11 + len(e.Type) + len(e.Reason) + len(e.Cause.Type) + len(e.Cause.Reason))
b.WriteString(msg)
b.WriteString(strconv.Itoa(e.Status))
if e.Type != "" {
b.WriteString(": ")
b.WriteString(e.Type)
}
if e.Reason != "" {
b.WriteString(": ")
b.WriteString(e.Reason)
}
if e.Cause.Type != "" {
b.WriteString(": ")
b.WriteString(e.Cause.Type)
}
if e.Cause.Reason != "" {
b.WriteString(": ")
b.WriteString(e.Cause.Reason)
}
return b.String()
}
var (
ErrElasticVersionConflict = errors.New("elastic version conflict")
ErrElasticNotFound = errors.New("elastic not found")
ErrInvalidBody = errors.New("invalid body")
ErrIndexNotFound = errors.New("index not found")
ErrTimeout = errors.New("timeout")
ErrNotFound = errors.New("not found")
knownErrorTypes = [3]string{
timeoutErrorType,
indexNotFoundErrorType,
versionConflictErrorType,
}
// helps with native translation of native java exceptions
// list possible exceptions is much broader, these we recognize.
// all listed here: https://github.com/elastic/elasticsearch/blob/f8d1d2afa67afd1b9769751fde35f86c5ec885d9/server/src/main/java/org/elasticsearch/ElasticsearchException.java#L730
errorTranslationMap = map[string]string{
ErrIndexNotFound.Error(): indexNotFoundErrorType,
"IndexNotFoundException": indexNotFoundErrorType,
ErrTimeout.Error(): timeoutErrorType,
"ElasticsearchTimeoutException": timeoutErrorType,
"ProcessClusterEventTimeoutException": timeoutErrorType,
"ReceiveTimeoutTransportException": timeoutErrorType,
ErrElasticVersionConflict.Error(): versionConflictErrorType,
"VersionConflictEngineException": versionConflictErrorType,
}
)
func TranslateError(status int, rawError json.RawMessage) error {
if status == 200 || status == 201 {
return nil
}
if len(rawError) == 0 {
// error was omitted
return &ErrElastic{
Status: status,
}
}
// try decoding detailed error by default
detailedError := &ErrorT{}
if err := json.Unmarshal(rawError, &detailedError); err == nil {
return translateDetailedError(status, detailedError)
}
reason := string(rawError)
eType := errType(reason)
switch eType {
case versionConflictErrorType:
return ErrElasticVersionConflict
default:
return &ErrElastic{
Status: status,
Type: eType,
Reason: reason,
}
}
}
func errType(errBody string) string {
for _, errCheck := range knownErrorTypes {
if strings.Contains(errBody, errCheck) {
return errCheck
}
}
for errCheck, errType := range errorTranslationMap {
if strings.Contains(errBody, errCheck) {
return errType
}
}
return unknownErrorType
}
func translateDetailedError(status int, e *ErrorT) error {
if e == nil {
return &ErrElastic{
Status: status,
}
}
var err error
switch e.Type {
case versionConflictErrorType:
err = ErrElasticVersionConflict
default:
err = &ErrElastic{
Status: status,
Type: e.Type,
Reason: e.Reason,
Cause: struct {
Type string
Reason string
}{
Type: e.Cause.Type,
Reason: e.Cause.Reason,
},
}
}
return err
}