Repository navigation
Expand file tree
/
Copy patherror.go
More file actions
85 lines (71 loc) · 1.84 KB
/
Copy patherror.go
File metadata and controls
85 lines (71 loc) · 1.84 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package privacy
// Error is a developer-friendly error wrapper that speaks privacy language.
// Its base error contains more technical details,
// and it can be enriched with meta-data, e.g., namespace and subject.
type Error struct {
// Err is the base err
Err error
msg, namespace, subject string
}
func newErr(msg string) Error {
return Error{
msg: msg,
}
}
// Message returns a short and primary message of the error.
//
// In contrast to Error() method, It doesn't include base error or meta-data in the return.
func (e Error) Message() string {
return e.msg
}
// Subject returns the associated subject to the error if it exists.
func (e Error) Subject() string {
if e.subject == "" {
if err, ok := e.Err.(Error); ok {
return err.Subject()
}
}
return e.subject
}
// Namespace returns the associated namespace to the error if it exists.
func (e Error) Namespace() string {
if e.namespace == "" {
if err, ok := e.Err.(Error); ok {
return err.Namespace()
}
}
return e.namespace
}
func (e Error) withNamespace(nspace string) Error {
e.namespace = nspace
return e
}
func (e Error) withSubject(sub string) Error {
e.subject = sub
return e
}
func (e Error) withBase(err error) Error {
e.Err = err
return e
}
// Error implements error interface.
// It intentionally omits namespace and subject to avoid leaking identifiers in logs or API responses.
// Use Namespace() and Subject() for structured access in internal debugging.
func (e Error) Error() string {
str := e.msg
if e.Err != nil {
str += ": " + e.Err.Error()
}
return str
}
func (e Error) Unwrap() error {
return e.Err
}
// Is compares only messages of Errors to decide whether they are equal.
// Otherwise, the wrapped error will decide.
func (e Error) Is(err error) bool {
if perr, ok := err.(Error); ok {
return e.msg == perr.msg
}
return false
}