-
Notifications
You must be signed in to change notification settings - Fork 0
/
publicerror.go
71 lines (57 loc) · 1.67 KB
/
publicerror.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
package httperror
import (
"bytes"
"errors"
"fmt"
"net/http"
"strconv"
)
// Public is an interface that requires a PublicMessage() string method.
// [httperror.PublicMessage] will extract the public error message from errors
// that implements this interface.
type Public = interface {
PublicMessage() string
}
// PublicMessage extracts the public message from errors that have a
// `PUblicMessage() string` method.
func PublicMessage(err error) string {
var publicError Public
if err == nil {
return ""
}
if errors.As(err, &publicError) {
return publicError.PublicMessage()
}
return ""
}
// NewPublic returns a new public error with the given status code and public
// error message generated using the format string and arguments. The
// resulting error value implements the the [httperror.Public] interface.
func NewPublic(status int, message string) error {
return publicError{message, httpError{status}}
}
// PublicErrorf returns a new public error with the given status code and
// public error message. The resulting value implements the the
// [httperror.Public] interface.
func PublicErrorf(status int, format string, args ...interface{}) error {
return publicError{fmt.Sprintf(format, args...), httpError{status}}
}
type publicError struct {
message string
httpError
}
// Error returns the text corresponding to this HTTP error status code.
func (e publicError) Error() string {
var b bytes.Buffer
b.WriteString(strconv.Itoa(e.status))
b.WriteString(" ")
b.WriteString(http.StatusText(e.status))
if e.message != "" {
b.WriteString(": ")
b.WriteString(e.message)
}
return b.String()
}
func (e publicError) PublicMessage() string {
return e.message
}