-
Notifications
You must be signed in to change notification settings - Fork 351
/
errors.go
178 lines (159 loc) · 4.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
package helpers
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
"strings"
"github.com/treeverse/lakefs/pkg/api"
)
var (
// ErrUnsupportedProtocol is the error returned when lakeFS server requests the client
// use a protocol it does not know about. Until recompiled to use a newer client,
// upload the object using through the lakeFS server.
ErrUnsupportedProtocol = errors.New("unsupported protocol")
// ErrRequestFailed is an error returned for failing lakeFS server replies.
ErrRequestFailed = errors.New("request failed")
ErrConflict = errors.New("conflict")
)
const minHTTPErrorStatusCode = 400
// isOK returns true if statusCode is an OK HTTP status code: 0-399.
func isOK(statusCode int) bool {
return statusCode < minHTTPErrorStatusCode
}
// APIFields are fields to use to format an HTTP error response that can be
// shown to the user.
type APIFields struct {
StatusCode int
Status string
Message string
}
// CallFailedError is an error performing the HTTP request itself formatted
// to be shown to a user. It does _not_ update its message when wrapped so
// usually should not be wrapped.
type CallFailedError struct {
Err error
Message string
}
func (e CallFailedError) Error() string {
wrapped := ""
if e.Err != nil {
wrapped = ": " + e.Err.Error()
}
return fmt.Sprintf("[%s]%s", e.Message, wrapped)
}
func (e CallFailedError) Unwrap() error {
return e.Err
}
// UserVisibleAPIError is an HTTP error response formatted to be shown to a
// user. It does _not_ update its message when wrapped so usually should
// not be wrapped.
type UserVisibleAPIError struct {
APIFields
Err error
}
// space stringifies non-nil elements from s... and returns all the
// non-empty resulting strings joined with spaces.
func spaced(s ...interface{}) string {
ret := make([]string, 0, len(s))
for _, t := range s {
if t != nil {
r := fmt.Sprint(t)
if r != "" {
ret = append(ret, r)
}
}
}
return strings.Join(ret, " ")
}
func (e UserVisibleAPIError) Error() string {
message := spaced(e.Message, e.Err.Error())
if message != "" {
message = ": " + message
}
return fmt.Sprintf("[%s]%s", e.Status, message)
}
func (e UserVisibleAPIError) Unwrap() error {
return e.Err
}
// ResponseAsError returns a UserVisibleAPIError wrapping an ErrRequestFailed
// wrapping a response from the server. It searches for a non-nil
// unsuccessful HTTPResponse field and uses its message, along with a Body
// that it assumes is an api.Error.
func ResponseAsError(response interface{}) error {
if response == nil {
return nil
}
if httpResponse, ok := response.(*http.Response); ok {
return HTTPResponseAsError(httpResponse)
}
r := reflect.Indirect(reflect.ValueOf(response))
if !r.IsValid() || r.Kind() != reflect.Struct {
return CallFailedError{
Message: fmt.Sprintf("bad type %s: must reference a struct", r.Type().Name()),
Err: ErrRequestFailed,
}
}
var ok bool
f := r.FieldByName("HTTPResponse")
if !f.IsValid() {
return fmt.Errorf("[no HTTPResponse]: %w", ErrRequestFailed)
}
httpResponse, ok := f.Interface().(*http.Response)
if !ok {
return fmt.Errorf("%w: no HTTPResponse", ErrRequestFailed)
}
if httpResponse == nil || isOK(httpResponse.StatusCode) {
return nil
}
statusCode := httpResponse.StatusCode
statusText := httpResponse.Status
if statusText == "" {
statusText = http.StatusText(statusCode)
}
var message string
f = r.FieldByName("Body")
if f.IsValid() && f.Type().Kind() == reflect.Slice && f.Type().Elem().Kind() == reflect.Uint8 {
body := f.Bytes()
var apiError api.Error
if json.Unmarshal(body, &apiError) == nil && apiError.Message != "" {
message = apiError.Message
}
}
return UserVisibleAPIError{
Err: ErrRequestFailed,
APIFields: APIFields{
StatusCode: statusCode,
Status: statusText,
Message: message,
},
}
}
func HTTPResponseAsError(httpResponse *http.Response) error {
if httpResponse == nil || isOK(httpResponse.StatusCode) {
return nil
}
statusCode := httpResponse.StatusCode
statusText := httpResponse.Status
if statusText == "" {
statusText = http.StatusText(statusCode)
}
var message string
body, err := io.ReadAll(httpResponse.Body)
if err == nil {
var apiError api.Error
if json.Unmarshal(body, &apiError) == nil && apiError.Message != "" {
message = apiError.Message
}
}
return UserVisibleAPIError{
Err: ErrRequestFailed,
APIFields: APIFields{
StatusCode: statusCode,
Status: statusText,
Message: message,
},
}
}