-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcause.go
More file actions
43 lines (33 loc) · 954 Bytes
/
cause.go
File metadata and controls
43 lines (33 loc) · 954 Bytes
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
package errors
import (
"errors"
)
type causeError struct{ err, cause error }
func (c *causeError) Error() string { return c.err.Error() + ": " + c.cause.Error() }
func (c *causeError) Unwrap() error { return c.cause }
func (c *causeError) As(target any) bool {
return errors.As(c.err, target) || errors.As(c.cause, target)
}
func (c *causeError) Is(target error) bool {
return errors.Is(c.err, target) || errors.Is(c.cause, target)
}
type causeError2 struct{ causeError }
// Cause wraps an error in a another error.
// This returns nil if `cause` is nil.
// If `err` is nil `cause` is returned.
func Cause(err, cause error) error {
if cause == nil {
return nil
}
if err == nil {
return cause
}
return &causeError2{causeError{err: err, cause: cause}}
}
// CauseStr like `Cause` but cause is a string instead of an error
func CauseStr(err error, cause string) error {
if err != nil {
err = Cause(err, New(cause))
}
return err
}