forked from databricks/databricks-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
189 lines (174 loc) · 5.46 KB
/
errors.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package apierr
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/xuxiaoshuo/databricks-sdk-go/logger"
)
var (
transientErrorStringMatches = []string{
"com.databricks.backend.manager.util.UnknownWorkerEnvironmentException",
"does not have any associated worker environments",
"There is no worker environment with id",
"Unknown worker environment",
"ClusterNotReadyException",
"connection reset by peer",
"TLS handshake timeout",
"connection refused",
"Unexpected error",
"i/o timeout",
}
)
// APIErrorBody maps "proper" databricks rest api errors to a struct
type APIErrorBody struct {
ErrorCode string `json:"error_code,omitempty"`
Message string `json:"message,omitempty"`
// The following two are for scim api only
// for RFC 7644 Section 3.7.3 https://tools.ietf.org/html/rfc7644#section-3.7.3
ScimDetail string `json:"detail,omitempty"`
ScimStatus string `json:"status,omitempty"`
ScimType string `json:"scimType,omitempty"`
API12Error string `json:"error,omitempty"`
}
// APIError is a generic struct for an api error on databricks
type APIError struct {
ErrorCode string
Message string
StatusCode int
}
// Error returns error message string instead of
func (apiError *APIError) Error() string {
return apiError.Message
}
// IsMissing tells if error is about missing resource
func IsMissing(err error) bool {
var apiError *APIError
if errors.As(err, &apiError) {
return apiError.IsMissing()
}
return false
}
// IsMissing tells if it is missing resource
func (apiError *APIError) IsMissing() bool {
return apiError.StatusCode == http.StatusNotFound
}
// IsTooManyRequests shows rate exceeded limits
func (apiError *APIError) IsTooManyRequests() bool {
return apiError.StatusCode == http.StatusTooManyRequests
}
// IsRetriable returns true if error is retriable
func (apiError *APIError) IsRetriable() bool {
return apiError.isRetriable(context.Background())
}
// isRetriable returns true if error is retriable
func (apiError *APIError) isRetriable(ctx context.Context) bool {
// Handle transient errors for retries
for _, substring := range transientErrorStringMatches {
if strings.Contains(apiError.Message, substring) {
logger.Debugf(ctx, "Attempting retry because of %#v", substring)
return true
}
}
// some API's recommend retries on HTTP 500, but we'll add that later
return false
}
// NotFound returns properly formatted Not Found error
func NotFound(message string) *APIError {
return &APIError{
ErrorCode: "NOT_FOUND",
StatusCode: 404,
Message: message,
}
}
// CheckForRetry inspects HTTP errors from the Databricks API for known transient errors on Workspace creation
func CheckForRetry(ctx context.Context, resp *http.Response, respErr error, body []byte, bodyErr error) (bool, error) {
if ue, ok := respErr.(*url.Error); ok {
apiError := &APIError{
ErrorCode: "IO_ERROR",
StatusCode: 523,
Message: ue.Error(),
}
return apiError.isRetriable(ctx), apiError
}
if resp == nil {
// If response is nil we can't make retry choices.
// In this case don't retry and return the original error from httpclient
return false, respErr
}
if resp.StatusCode == 429 {
return true, &APIError{
ErrorCode: "TOO_MANY_REQUESTS",
Message: "Current request has to be retried",
StatusCode: 429,
}
}
if resp.StatusCode >= 400 {
apiError := parseErrorFromResponse(resp, body, bodyErr)
return apiError.isRetriable(ctx), apiError
}
return false, respErr
}
func parseErrorFromResponse(resp *http.Response, body []byte, err error) *APIError {
if err != nil {
return &APIError{
Message: err.Error(),
ErrorCode: "IO_READ",
StatusCode: resp.StatusCode,
}
}
// try to read in nicely formatted API error response
var errorBody APIErrorBody
err = json.Unmarshal(body, &errorBody)
if err != nil {
errorBody = parseUnknownError(resp.Status, body, err)
}
if errorBody.API12Error != "" {
// API 1.2 has different response format, let's adapt
errorBody.Message = errorBody.API12Error
}
// Handle SCIM error message details
if errorBody.Message == "" && errorBody.ScimDetail != "" {
if errorBody.ScimDetail == "null" {
errorBody.Message = "SCIM API Internal Error"
} else {
errorBody.Message = errorBody.ScimDetail
}
// add more context from SCIM responses
errorBody.Message = fmt.Sprintf("%s %s", errorBody.ScimType, errorBody.Message)
errorBody.Message = strings.Trim(errorBody.Message, " ")
errorBody.ErrorCode = fmt.Sprintf("SCIM_%s", errorBody.ScimStatus)
}
return &APIError{
Message: errorBody.Message,
ErrorCode: errorBody.ErrorCode,
StatusCode: resp.StatusCode,
}
}
func parseUnknownError(status string, body []byte, err error) (errorBody APIErrorBody) {
// this is most likely HTML... since un-marshalling JSON failed
// Status parts first in case html message is not as expected
statusParts := strings.SplitN(status, " ", 2)
if len(statusParts) < 2 {
errorBody.ErrorCode = "UNKNOWN"
} else {
errorBody.ErrorCode = strings.ReplaceAll(
strings.ToUpper(strings.Trim(statusParts[1], " .")),
" ", "_")
}
stringBody := string(body)
messageRE := regexp.MustCompile(`<pre>(.*)</pre>`)
messageMatches := messageRE.FindStringSubmatch(stringBody)
// No messages with <pre> </pre> format found so return a APIError
if len(messageMatches) < 2 {
errorBody.Message = fmt.Sprintf("Response from server (%s) %s: %v",
status, stringBody, err)
return
}
errorBody.Message = strings.Trim(messageMatches[1], " .")
return
}