-
Notifications
You must be signed in to change notification settings - Fork 37
/
error_override.go
73 lines (62 loc) · 2.21 KB
/
error_override.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
package apierr
import (
"context"
"net/http"
"regexp"
"strconv"
"github.com/databricks/databricks-sdk-go/logger"
)
// errorOverride is a struct that allows for customizing error responses from
// the API. This is necessary to ensure consistency between different APIs that
// currently have different error behavior.
//
// The actual overrides themselves are autogenerated in error_overrides.go.
type errorOverride struct {
// The name of the override. This is used for logging purposes.
debugName string
// pathRegex is a substring that must be present in the request path for
// this override to be applied.
pathRegex *regexp.Regexp
// verb is the HTTP verb that must be used for this override to be applied.
verb string
// statusCodeMatcher is a regular expression that must match the status code
// for this override to be applied. If nil, this field is ignored.
statusCodeMatcher *regexp.Regexp
// errorCodeMatcher is a regular expression that must match the error code
// for this override to be applied. If nil, this field is ignored.
errorCodeMatcher *regexp.Regexp
// messageMatcher is a regular expression that must match the error message
// for this override to be applied. If nil, this field is ignored.
messageMatcher *regexp.Regexp
// customError is the error that will be returned by APIError.Unwrap() if
// this override is applied.
customError error
}
func (e *errorOverride) matches(err *APIError, resp *http.Response) bool {
if e.verb != resp.Request.Method {
return false
}
if e.pathRegex != nil && !e.pathRegex.MatchString(resp.Request.URL.Path) {
return false
}
statusCode := strconv.Itoa(resp.StatusCode)
if e.statusCodeMatcher != nil && !e.statusCodeMatcher.MatchString(statusCode) {
return false
}
if e.errorCodeMatcher != nil && !e.errorCodeMatcher.MatchString(err.ErrorCode) {
return false
}
if e.messageMatcher != nil && !e.messageMatcher.MatchString(err.Message) {
return false
}
return true
}
func applyOverrides(ctx context.Context, err *APIError, resp *http.Response) {
for _, override := range allOverrides {
if override.matches(err, resp) {
logger.Debugf(ctx, "Applying error override: %s", override.debugName)
err.unwrap = override.customError
return
}
}
}