-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
89 lines (75 loc) · 2.14 KB
/
error.go
File metadata and controls
89 lines (75 loc) · 2.14 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
86
87
88
89
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package haelu
import "errors"
// SelfStatuser is an optional interface that an error can implement
// to indicate its health status.
type SelfStatuser interface {
// Status returns the health status for this error. Note that this allows
// errors to indicate a healthy status.
Status() Status
}
// SelfBooler is an alternative to SelfStatuser that lets an error simply
// indicate good or bad health.
type SelfBooler interface {
// Status indicates the health status for this error. If this method returns false,
// StatusBad is assumed. If this method returns true, StatusGood is assumed.
// Note that this allows errors to indicate a healthy status.
Status() bool
}
type statusError struct {
err error
status Status
}
func (se *statusError) Error() string {
return se.err.Error()
}
func (se *statusError) Unwrap() error {
return se.err
}
func (se *statusError) Status() Status {
return se.status
}
// AddStatus associates a health Status with the given error. The
// returned error will wrap err and implement SelfStatuser.
//
// If err already has a status associated with it, it will be
// replaced with the given status.
func AddStatus(err error, status Status) error {
return &statusError{
err: err,
status: status,
}
}
// ErrorStatus examines an error to determine what health Status to
// associated with it.
//
// If err is nil, this function returns StatusGood.
//
// If err implements SelfStatuser, then the result of SelfStatuser.Status() is returned.
//
// If err implements SelfBooler, then StatusGood or StatusBad is returned
// based on the return value of SelfBooler.Status().
//
// For a non-nil error that does not implement one of the optional
// interfaces in this package, this function returns StatusBad.
func ErrorStatus(err error) Status {
var (
s SelfStatuser
b SelfBooler
)
switch {
case err == nil:
return StatusGood
case errors.As(err, &s):
return s.Status()
case errors.As(err, &b):
if b.Status() {
return StatusGood
} else {
return StatusBad
}
default:
return StatusBad
}
}