-
Notifications
You must be signed in to change notification settings - Fork 1
/
nil-error.go
70 lines (58 loc) · 1.93 KB
/
nil-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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"fmt"
"github.com/haraldrudell/parl/perrors"
"github.com/haraldrudell/parl/pruntime"
)
const (
// count frame of [parl.NilError]
nilErrorFrames = 1
)
// ErrNil is used with [errors.Is] to detect that a panic or error value was caused
// by a value that cannot be nil, such as a function argument to a new function,
// was nil
// - —
// - a nilValue type implements:
// - — a dummy NilValueError method
// - — an Is method returning true for errors implementing a NilValueError method
//
// Usage:
//
// if errors.Is(err, parl.ErrNil) { …
var ErrNil = &nilValue{"value"}
// NilError returns an error used with panic() indicating that a value that cannot be nil,
// such as a function argument to a new function, was nil
// - such panics typically indicate compile-time issues with code
//
// Usage:
//
// func NewX(xValue *int) (x *X) {
// if xValue == nil {
// panic(parl.NilError("xValue")) // “somePackage.NewX xValue cannot be nil”
func NilError(valueName string) (err error) {
var cL = pruntime.NewCodeLocation(nilErrorFrames)
return perrors.Stackn(&nilValue{
s: fmt.Sprintf("%s %s cannot be nil",
cL.PackFunc(), // “somePackage.NewX”
valueName, // “xValue”
)}, nilErrorFrames)
}
var _ nilValueIface = &nilValue{}
// nilValue is the type for NilError errors
type nilValue struct{ s string }
// nilValueIface looks for the NilValueError method
type nilValueIface interface{ NilValueError() }
// Error return the error message
// - allows nilValue to implement the error interface
func (e *nilValue) Error() (message string) { return e.s }
// Is method allowing for nilValue values to detect other nilValues
func (e *nilValue) Is(err error) (is bool) {
_, is = err.(nilValueIface)
return
}
// dummy method NilValueError
func (e *nilValue) NilValueError() {}