-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
52 lines (39 loc) · 1.15 KB
/
error.go
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 optional
import (
"github.com/pkg/errors"
)
// The RuntimeError interface identifies a run time error.
type RuntimeError interface {
error
// runtimeError is a no-op function but serves to distinguish types
// that are run time errors from ordinary errors.
runtimeError()
}
// IsRuntimeError indicates whether an error is RuntimeError type.
func IsRuntimeError(err error) bool {
_, ok := err.(RuntimeError)
return ok
}
type illegalStatusError struct{}
func (e *illegalStatusError) Error() string {
return "illegal status"
}
func (e *illegalStatusError) runtimeError() {}
type nilPointerError struct{}
func (e *nilPointerError) Error() string {
return "nil point"
}
func (e *nilPointerError) runtimeError() {}
func checkNotNil(value interface{}) interface{} {
return checkNotNilWithMessage(value, "")
}
func checkNotNilWithMessage(value interface{}, errorMessage string) interface{} {
if value == nil {
panic(errors.Wrap(&nilPointerError{}, errorMessage))
}
return value
}
// IsCausedByRuntimeError indicates whether an error is caused by RuntimeError.
func IsCausedByRuntimeError(err error) bool {
return IsRuntimeError(errors.Cause(err))
}