-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
93 lines (78 loc) · 2.21 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
package nvelope
import (
"encoding"
"errors"
"net/http"
"github.com/muir/nject"
)
// MinimalErrorHandler provides a way to catch returned error values from
// the many functions that return them if MakeResponseEncoder is not used.
// http.ResponseWriter is used instead of a DeferredWriter. That means that
// MinimalErrorHandler cannot know if a response has already been made. The
// assumption is that if the returned error is nil, a respons has been made
// and if the returned error is not nil, then a response has not yet been
// made and the MinimalErrorHandler should make one. GetReturnCode is used
// to determine the return code.
var MinimalErrorHandler = nject.Provide("minimal-error-handler", minimalErrorHandler)
func minimalErrorHandler(inner func() error, w http.ResponseWriter) {
err := inner()
if err == nil {
return
}
w.WriteHeader(GetReturnCode(err))
_, _ = w.Write([]byte(err.Error()))
}
// ReturnCode associates an HTTP return code with a error.
// if err is nil, then nil is returned.
func ReturnCode(err error, code int) error {
if err == nil {
return nil
}
return returnCode{
cause: err,
code: code,
}
}
type returnCode struct {
cause error
code int
}
func (err returnCode) Unwrap() error {
return err.cause
}
func (err returnCode) Cause() error {
return err.cause
}
func (err returnCode) Error() string {
return err.cause.Error()
}
// NotFound annotates an error has giving 404 HTTP return code
func NotFound(err error) error {
return ReturnCode(err, 404)
}
// BadRequest annotates an error has giving 400 HTTP return code
func BadRequest(err error) error {
return ReturnCode(err, 400)
}
// Unauthorized annotates an error has giving 401 HTTP return code
func Unauthorized(err error) error {
return ReturnCode(err, 401)
}
// Forbidden annotates an error has giving 403 HTTP return code
func Forbidden(err error) error {
return ReturnCode(err, 403)
}
// GetReturnCode turns an error into an HTTP response code.
func GetReturnCode(err error) int {
var rc returnCode
if errors.As(err, &rc) {
return rc.code
}
return 500
}
// CanModel represents errors that can transform themselves into a model
// for logging.
type CanModel interface {
error
Model() encoding.TextUnmarshaler
}