-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
86 lines (74 loc) · 2.21 KB
/
errors.go
File metadata and controls
86 lines (74 loc) · 2.21 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
package browserpm
import "fmt"
// ErrorCode represents the category of a browserpm error.
type ErrorCode int
const (
ErrSessionNotFound ErrorCode = iota
ErrSessionExists
ErrPoolExhausted
ErrContextDead
ErrPageUnavailable
ErrTimeout
ErrClosed
ErrInvalidState
ErrInternal
)
var errorCodeNames = map[ErrorCode]string{
ErrSessionNotFound: "SessionNotFound",
ErrSessionExists: "SessionExists",
ErrPoolExhausted: "PoolExhausted",
ErrContextDead: "ContextDead",
ErrPageUnavailable: "PageUnavailable",
ErrTimeout: "Timeout",
ErrClosed: "Closed",
ErrInvalidState: "InvalidState",
ErrInternal: "Internal",
}
func (c ErrorCode) String() string {
if name, ok := errorCodeNames[c]; ok {
return name
}
return fmt.Sprintf("ErrorCode(%d)", int(c))
}
// BpmError is the standard error type for browserpm.
type BpmError struct {
Code ErrorCode
Message string
Cause error
}
func (e *BpmError) Error() string {
if e.Cause != nil {
return fmt.Sprintf("browserpm [%s]: %s: %v", e.Code, e.Message, e.Cause)
}
return fmt.Sprintf("browserpm [%s]: %s", e.Code, e.Message)
}
func (e *BpmError) Unwrap() error {
return e.Cause
}
// Is supports errors.Is matching by ErrorCode.
func (e *BpmError) Is(target error) bool {
if t, ok := target.(*BpmError); ok {
return e.Code == t.Code
}
return false
}
// NewError creates a BpmError without a cause.
func NewError(code ErrorCode, msg string) *BpmError {
return &BpmError{Code: code, Message: msg}
}
// WrapError creates a BpmError wrapping an underlying cause.
func WrapError(err error, code ErrorCode, msg string) *BpmError {
return &BpmError{Code: code, Message: msg, Cause: err}
}
// Sentinel errors for use with errors.Is.
var (
ErrSessionNotFoundErr = &BpmError{Code: ErrSessionNotFound}
ErrSessionExistsErr = &BpmError{Code: ErrSessionExists}
ErrPoolExhaustedErr = &BpmError{Code: ErrPoolExhausted}
ErrContextDeadErr = &BpmError{Code: ErrContextDead}
ErrPageUnavailableErr = &BpmError{Code: ErrPageUnavailable}
ErrTimeoutErr = &BpmError{Code: ErrTimeout}
ErrClosedErr = &BpmError{Code: ErrClosed}
ErrInvalidStateErr = &BpmError{Code: ErrInvalidState}
ErrInternalErr = &BpmError{Code: ErrInternal}
)