-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
52 lines (44 loc) · 1.37 KB
/
errors.go
File metadata and controls
52 lines (44 loc) · 1.37 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
package conditional
import (
"errors"
"fmt"
)
// ErrorKind classifies conditional failures without depending on exact server
// error text.
type ErrorKind string
const (
// ErrorKindParse indicates that the expression could not be parsed.
ErrorKindParse ErrorKind = "parse"
// ErrorKindValidation indicates that validation failed before evaluation.
ErrorKindValidation ErrorKind = "validation"
// ErrorKindEvaluation indicates that evaluation failed.
ErrorKindEvaluation ErrorKind = "evaluation"
// ErrorKindResult indicates that the expression did not evaluate to a bool.
ErrorKindResult ErrorKind = "result"
)
// Error is a typed conditional error. Cause contains a lower-level error when
// one is useful to expose through Unwrap.
type Error struct {
Kind ErrorKind
Message string
Cause error
}
func (e *Error) Error() string {
if e.Message == "" {
return string(e.Kind)
}
return fmt.Sprintf("%s: %s", e.Kind, e.Message)
}
// Unwrap returns the underlying cause, if any.
func (e *Error) Unwrap() error {
return e.Cause
}
// Is reports whether err has the same error kind as target.
func (e *Error) Is(target error) bool {
targetError, ok := target.(*Error)
return ok && e.Kind == targetError.Kind
}
// IsErrorKind reports whether err contains a conditional Error with kind.
func IsErrorKind(err error, kind ErrorKind) bool {
return errors.Is(err, &Error{Kind: kind})
}