/
errors.go
88 lines (67 loc) · 2.16 KB
/
errors.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package starlarktruth
import (
"fmt"
"strings"
"go.starlark.net/starlark"
)
// InvalidAssertion signifies an invalid assertion was attempted
// such as comparing with None.
type InvalidAssertion string
var _ error = InvalidAssertion("")
func newInvalidAssertion(prop string) InvalidAssertion { return InvalidAssertion(prop) }
func (e InvalidAssertion) Error() string { return string(e) }
// TruthAssertion signifies an assertion predicate was invalidated.
type TruthAssertion string
var _ error = TruthAssertion("")
func newTruthAssertion(msg string) TruthAssertion { return TruthAssertion(msg) }
func (e TruthAssertion) Error() string { return string(e) }
// unhandled internal & public errors
const errUnhandled = unhandledError(0)
type unhandledError int
var _ error = errUnhandled
func (e unhandledError) Error() string { return "unhandled" }
// UnhandledError appears when an operation on an incompatible type is attempted.
type UnhandledError struct {
name string
actual starlark.Value
args starlark.Tuple
}
var _ error = (*UnhandledError)(nil)
func (t *T) unhandled(name string, args ...starlark.Value) *UnhandledError {
return &UnhandledError{
name: name,
actual: t.actual,
args: args,
}
}
func (e UnhandledError) Error() string {
var b strings.Builder
b.WriteString("Invalid assertion .")
b.WriteString(e.name)
b.WriteByte('(')
for i, arg := range e.args {
if i != 0 {
b.WriteString(", ")
}
b.WriteString(arg.String())
}
b.WriteString(") on value of type ")
b.WriteString(e.actual.Type())
return b.String()
}
// UnresolvedError describes that an `assert.that(actual)` was called but never any of its `.truth_methods(subject)`.
// At the exception of (as each by themselves this still require an assertion):
// * `.named(name)`
// * `.is_within(tolerance)`
// * `.is_not_within(tolerance)`
type UnresolvedError struct {
// Msg is the error string
Msg string
}
var _ error = &UnresolvedError{}
func (e *UnresolvedError) Error() string { return e.Msg }
func newUnresolvedError(e string) *UnresolvedError {
return &UnresolvedError{
Msg: fmt.Sprintf("%s: %s.%s(...) is missing an assertion", e, Module, Method),
}
}