-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
46 lines (36 loc) · 1.08 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
package containers
import (
"context"
"fmt"
"net/http"
)
type ErrHandler interface {
HandleErr(w http.ResponseWriter, r *http.Request, err error)
}
type redirectErrorHandler struct {
url string
code int
}
func (reh *redirectErrorHandler) HandleErr(w http.ResponseWriter, r *http.Request, err error) {
http.Redirect(w, r, reh.url, reh.code)
}
func (reh *redirectErrorHandler) Error() string {
return fmt.Sprintf("%d: redirect to: %s", reh.code, reh.url)
}
func NewRedirectError(url string, code int) (err error) {
err = &redirectErrorHandler{url: url, code: code}
return
}
const internalServerErrorHandlerKey = "containers.InternalServerErrorHandler"
type internalServerErrorHandler struct {
h http.Handler
eh ErrHandler
}
func (iseh *internalServerErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(context.WithValue(r.Context(), internalServerErrorHandlerKey, iseh.eh))
iseh.h.ServeHTTP(w, r)
}
func UseErrHandler(h http.Handler, errhandler ErrHandler) (handler http.Handler) {
handler = &internalServerErrorHandler{h: h, eh: errhandler}
return
}