-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpublic.go
More file actions
125 lines (102 loc) · 2.74 KB
/
Copy pathpublic.go
File metadata and controls
125 lines (102 loc) · 2.74 KB
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
package errors
import (
"net/http"
)
type (
publicError struct {
cause error
message string
}
// PublicError is the type code and client message for an error, as
// returned by [Public]. It serializes without the error's internal text,
// for example as JSON:
//
// {"code":"USER_NOT_FOUND","message":"The user could not be found"}
PublicError struct {
Code string `json:"code" xml:"code" yaml:"code" msgpack:"code"`
Message string `json:"message" xml:"message" yaml:"message" msgpack:"message"`
}
)
func (e publicError) Error() string {
return e.cause.Error()
}
func (e publicError) Unwrap() error {
return e.cause
}
// Public returns err's type code and client-facing message. For nil, the code
// is "OK" and the message is empty.
func Public(err error) PublicError {
return PublicError{
Code: TypeCode(err),
Message: PublicMessage(err),
}
}
// WrapPublicMessage sets the message that [PublicMessage] returns for err,
// for cases where one error needs a message its kind or category doesn't
// provide. err's text, codes, and identity don't change. If err is later
// wrapped by a category or kind, that outer classification's message is used
// instead. WrapPublicMessage returns nil for a nil err and panics when
// message is empty for a non-nil err.
func WrapPublicMessage(err error, message string) error {
if err == nil {
return nil
}
if message == "" {
panic("errors: public message cannot be empty")
}
return &publicError{
cause: err,
message: message,
}
}
// PublicMessage returns a message for err that is safe to show clients. It
// never returns err.Error(). It uses the first of these that applies:
//
// 1. A message set with [WrapPublicMessage].
// 2. The message set with [WithPublicMessage] on err's [Kind].
// 3. "Internal Server Error" for a 5xx HTTP code or an unclassified error.
// 4. The HTTP status text for err's code, such as "Not Found".
//
// It returns an empty string for nil.
func PublicMessage(err error) string {
if err == nil {
return ""
}
if msg, ok := publicMessageOverride(err); ok {
return msg
}
c, ok := resolveClassification(err)
if !ok {
return "Internal Server Error"
}
if c.publicMessage != "" {
return c.publicMessage
}
if c.httpCode >= 500 {
return "Internal Server Error"
}
msg := http.StatusText(c.httpCode)
if msg == "" {
return "Request failed"
}
return msg
}
func publicMessageOverride(err error) (string, bool) {
for err != nil {
if e, ok := err.(*publicError); ok {
return e.message, true
}
if _, ok := err.(classified); ok {
return "", false
}
if _, ok := err.(multiUnwrapper); ok {
return "", false
}
wrapped, ok := err.(singleUnwrapper)
if !ok {
return "", false
}
err = wrapped.Unwrap()
}
return "", false
}