forked from jsteenb2/mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
64 lines (54 loc) · 1.59 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
60
61
62
63
64
package allsrv
import (
"github.com/jsteenb2/errors"
)
const (
ErrKindExists = errors.Kind("exists")
ErrKindInvalid = errors.Kind("invalid")
ErrKindNotFound = errors.Kind("not found")
ErrKindUnAuthed = errors.Kind("unauthorized")
ErrKindInternal = errors.Kind("internal")
)
const (
errCodeExist = 1
errCodeInvalid = 2
errCodeNotFound = 3
errCodeUnAuthed = 4
errCodeInternal = 5
)
func errCode(kind error) int {
switch {
case errors.Is(kind, ErrKindExists):
return errCodeExist
case errors.Is(kind, ErrKindInvalid):
return errCodeInvalid
case errors.Is(kind, ErrKindNotFound):
return errCodeNotFound
case errors.Is(kind, ErrKindUnAuthed):
return errCodeUnAuthed
case errors.Is(kind, ErrKindInternal):
return errCodeInternal
default:
return errCode(ErrKindInternal)
}
}
var (
errIDRequired = InvalidErr("id is required")
)
// ExistsErr creates an exists error.
func ExistsErr(msg string, fields ...any) error {
return errors.New(msg, errors.KVs(fields...), ErrKindExists, errors.SkipCaller)
}
func InvalidErr(msg string, fields ...any) error {
return errors.New(msg, errors.KVs(fields...), ErrKindInvalid, errors.SkipCaller)
}
func InternalErr(msg string, fields ...any) error {
return errors.New(msg, errors.KVs(fields...), ErrKindInternal, errors.SkipCaller)
}
// NotFoundErr creates a not found error.
func NotFoundErr(msg string, fields ...any) error {
return errors.New(msg, errors.KVs(fields...), ErrKindNotFound, errors.SkipCaller)
}
func unauthedErr(msg string, fields ...any) error {
return errors.New(msg, errors.KVs(fields...), ErrKindUnAuthed, errors.SkipCaller)
}