-
Notifications
You must be signed in to change notification settings - Fork 7
/
errs.go
44 lines (37 loc) · 1.16 KB
/
errs.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
// Package errs provides types and support related to web v1 functionality.
package errs
import "errors"
// Response is the form used for API responses from failures in the API.
type Response struct {
Error string `json:"error"`
Fields map[string]string `json:"fields,omitempty"`
}
// Trusted is used to pass an error during the request through the
// application with web specific context.
type Trusted struct {
Err error
Status int
}
// NewTrusted wraps a provided error with an HTTP status code. This
// function should be used when handlers encounter expected errors.
func NewTrusted(err error, status int) error {
return &Trusted{err, status}
}
// Error implements the error interface. It uses the default message of the
// wrapped error. This is what will be shown in the services' logs.
func (te *Trusted) Error() string {
return te.Err.Error()
}
// IsTrusted checks if an error of type TrustedError exists.
func IsTrusted(err error) bool {
var te *Trusted
return errors.As(err, &te)
}
// GetTrusted returns a copy of the TrustedError pointer.
func GetTrusted(err error) *Trusted {
var te *Trusted
if !errors.As(err, &te) {
return nil
}
return te
}