-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
231 lines (175 loc) · 6.2 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Copyright 2022-2024 EscherCloud.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package errors
import (
"encoding/json"
"errors"
"net/http"
"github.com/eschercloudai/unikorn/pkg/server/generated"
"sigs.k8s.io/controller-runtime/pkg/log"
)
var (
// ErrRequest is raised for all handler errors.
ErrRequest = errors.New("request error")
)
// HTTPError wraps ErrRequest with more contextual information that is used to
// propagate and create suitable responses.
type HTTPError struct {
// status is the HTTP error code.
status int
// code us the terse error code to return to the client.
code generated.Oauth2ErrorError
// description is a verbose description to log/return to the user.
description string
// err is set when the originator was an error. This is only used
// for logging so as not to leak server internals to the client.
err error
// values are arbitrary key value pairs for logging.
values []interface{}
}
// newHTTPError returns a new HTTP error.
func newHTTPError(status int, code generated.Oauth2ErrorError, description string) *HTTPError {
return &HTTPError{
status: status,
code: code,
description: description,
}
}
// WithError augments the error with an error from a library.
func (e *HTTPError) WithError(err error) *HTTPError {
e.err = err
return e
}
// WithValues augments the error with a set of K/V pairs.
// Values should not use the "error" key as that's implicitly defined
// by WithError and could collide.
func (e *HTTPError) WithValues(values ...interface{}) *HTTPError {
e.values = values
return e
}
// Unwrap implements Go 1.13 errors.
func (e *HTTPError) Unwrap() error {
return ErrRequest
}
// Error implements the error interface.
func (e *HTTPError) Error() string {
return e.description
}
// Write returns the error code and description to the client.
func (e *HTTPError) Write(w http.ResponseWriter, r *http.Request) {
// Log out any detail from the error that shouldn't be
// reported to the client. Do it before things can error
// and return.
log := log.FromContext(r.Context())
var details []interface{}
if e.description != "" {
details = append(details, "detail", e.description)
}
if e.err != nil {
details = append(details, "error", e.err)
}
if e.values != nil {
details = append(details, e.values...)
}
log.Info("error detail", details...)
// Emit the response to the client.
w.Header().Add("Cache-Control", "no-cache")
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(e.status)
// Emit the response body.
ge := &generated.Oauth2Error{
Error: e.code,
ErrorDescription: e.description,
}
body, err := json.Marshal(ge)
if err != nil {
log.Error(err, "failed to marshal error response")
return
}
if _, err := w.Write(body); err != nil {
log.Error(err, "failed to wirte error response")
return
}
}
func HTTPForbidden(description string) *HTTPError {
return newHTTPError(http.StatusForbidden, generated.Forbidden, description)
}
func HTTPNotFound() *HTTPError {
return newHTTPError(http.StatusNotFound, generated.NotFound, "resource not found")
}
func IsHTTPNotFound(err error) bool {
httpError := &HTTPError{}
if ok := errors.As(err, &httpError); !ok {
return false
}
if httpError.status != http.StatusNotFound {
return false
}
return true
}
func HTTPMethodNotAllowed() *HTTPError {
return newHTTPError(http.StatusMethodNotAllowed, generated.MethodNotAllowed, "the requested method was not allowed")
}
func HTTPConflict() *HTTPError {
return newHTTPError(http.StatusConflict, generated.Conflict, "the requested resource already exists")
}
// OAuth2InvalidRequest indicates a client error.
func OAuth2InvalidRequest(description string) *HTTPError {
return newHTTPError(http.StatusBadRequest, generated.InvalidRequest, description)
}
func OAuth2UnauthorizedClient(description string) *HTTPError {
return newHTTPError(http.StatusBadRequest, generated.UnauthorizedClient, description)
}
func OAuth2UnsupportedGrantType(description string) *HTTPError {
return newHTTPError(http.StatusBadRequest, generated.UnsupportedGrantType, description)
}
func OAuth2InvalidGrant(description string) *HTTPError {
return newHTTPError(http.StatusBadRequest, generated.InvalidGrant, description)
}
func OAuth2InvalidClient(description string) *HTTPError {
return newHTTPError(http.StatusBadRequest, generated.InvalidClient, description)
}
// OAuth2AccessDenied tells the client the authentication failed e.g.
// username/password are wrong, or a token has expired and needs reauthentication.
func OAuth2AccessDenied(description string) *HTTPError {
return newHTTPError(http.StatusUnauthorized, generated.AccessDenied, description)
}
// OAuth2ServerError tells the client we are at fault, this should never be seen
// in production. If so then our testing needs to improve.
func OAuth2ServerError(description string) *HTTPError {
return newHTTPError(http.StatusInternalServerError, generated.ServerError, description)
}
// OAuth2InvalidScope tells the client it doesn't have the necessary scope
// to access the resource.
func OAuth2InvalidScope(description string) *HTTPError {
return newHTTPError(http.StatusUnauthorized, generated.InvalidScope, description)
}
// toHTTPError is a handy unwrapper to get a HTTP error from a generic one.
func toHTTPError(err error) *HTTPError {
var httpErr *HTTPError
if !errors.As(err, &httpErr) {
return nil
}
return httpErr
}
// HandleError is the top level error handler that should be called from all
// path handlers on error.
func HandleError(w http.ResponseWriter, r *http.Request, err error) {
log := log.FromContext(r.Context())
if httpError := toHTTPError(err); httpError != nil {
httpError.Write(w, r)
return
}
log.Error(err, "unhandled error")
OAuth2ServerError("unhandled error").Write(w, r)
}