Repository navigation
Expand file tree
/
Copy pathhelpers.go
More file actions
61 lines (50 loc) · 1.51 KB
/
Copy pathhelpers.go
File metadata and controls
61 lines (50 loc) · 1.51 KB
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
package errors
import (
stderrors "errors"
"github.com/jgolang/errors/codes"
)
// CodeOf returns the first non-nil jgolang error code found in err's error
// chain, including codes behind other wrappers such as fmt.Errorf.
func CodeOf(err error) codes.Coder {
for err != nil {
var wrapped *Error
// errors.As also matches a typed-nil *Error, which has no code or cause.
if !stderrors.As(err, &wrapped) || wrapped == nil {
return nil
}
if wrapped.Code != nil {
return wrapped.Code
}
// A nil code must not hide a code deeper in the chain.
err = wrapped.cause
}
return nil
}
// KindOf returns the kind declared by CodeOf(err), or an empty Kind when that
// code declares none or err has no code. Reading the kind from the same code
// as CodeOf keeps a status derived from it consistent with the reported code.
func KindOf(err error) codes.Kind {
if kinded, ok := CodeOf(err).(codes.Kinded); ok {
return kinded.Kind()
}
return ""
}
// codeOrUnknown returns the code found in err's chain, or codes.Unknown.
func codeOrUnknown(err error) codes.Coder {
if code := CodeOf(err); code != nil {
return code
}
return codes.Unknown
}
// Is reports whether any error in err's chain matches target.
func Is(err, target error) bool {
return stderrors.Is(err, target)
}
// PublicMessage returns a user-safe message for err without exposing technical details.
func PublicMessage(err error) string {
code := CodeOf(err)
if code == nil || code.Msg() == "" {
return codes.Unknown.Msg()
}
return code.Msg()
}