-
Notifications
You must be signed in to change notification settings - Fork 8
/
errors.go
47 lines (41 loc) · 923 Bytes
/
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
package allsrv
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 isErrType(err error, want int) bool {
e, _ := err.(Err)
return err != nil && e.Type == want
}