-
Notifications
You must be signed in to change notification settings - Fork 8
/
errors.go
59 lines (50 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
47
48
49
50
51
52
53
54
55
56
57
58
59
package allsrv
import (
"errors"
)
const (
errTypeUnknown = iota
errTypeExists
errTypeInvalid
errTypeNotFound
errTypeUnAuthed
errTypeInternal
)
// Err provides a lightly structured error that we can attach behavior. Additionally,
// the use of fields makes it possible for us to enrich our logging infra without
// blowing up the message cardinality.
type Err struct {
Type int
Msg string
Fields []any
}
// Error returns the error message.
func (e Err) Error() string {
return e.Msg
}
// ExistsErr creates an exists error.
func ExistsErr(msg string, fields ...any) error {
return Err{
Type: errTypeExists,
Msg: msg,
Fields: fields,
}
}
// NotFoundErr creates a not found error.
func NotFoundErr(msg string, fields ...any) error {
return Err{
Type: errTypeNotFound,
Msg: msg,
Fields: fields,
}
}
func IsNotFoundErr(err error) bool {
return isErrType(err, errTypeNotFound)
}
func IsExistsErr(err error) bool {
return isErrType(err, errTypeExists)
}
func isErrType(err error, want int) bool {
var aErr Err
return errors.As(err, &aErr) && aErr.Type == want
}