/
errors.go
153 lines (132 loc) · 3.64 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package query
import (
"bytes"
"errors"
"fmt"
)
// ErrNoContext is a common query error where a query attempts to reference a
// structured field when there is no context.
type ErrNoContext struct {
FieldName string
}
// Error returns an attempt at a useful error message.
func (e ErrNoContext) Error() string {
if e.FieldName != "" {
return fmt.Sprintf("context was undefined, unable to reference `%v`", e.FieldName)
}
return "context was undefined"
}
//------------------------------------------------------------------------------
// TypeError represents an error where a value of a type was required for a
// function, method or operator but instead a different type was found.
type TypeError struct {
From string
Expected []ValueType
Actual ValueType
Value string
}
// Error implements the standard error interface for TypeError.
func (t *TypeError) Error() string {
var errStr bytes.Buffer
if len(t.Expected) > 0 {
errStr.WriteString("expected ")
for i, exp := range t.Expected {
if i > 0 {
if len(t.Expected) > 2 && i < (len(t.Expected)-1) {
errStr.WriteString(", ")
} else {
errStr.WriteString(" or ")
}
}
errStr.WriteString(string(exp))
}
errStr.WriteString(" value")
} else {
errStr.WriteString("unexpected value")
}
fmt.Fprintf(&errStr, ", got %v", t.Actual)
if t.From != "" {
fmt.Fprintf(&errStr, " from %v", t.From)
}
if t.Value != "" {
fmt.Fprintf(&errStr, " (%v)", t.Value)
}
return errStr.String()
}
// NewTypeError creates a new type error.
func NewTypeError(value interface{}, exp ...ValueType) *TypeError {
return NewTypeErrorFrom("", value, exp...)
}
// NewTypeErrorFrom creates a new type error with an annotation of the query
// that provided the wrong type.
func NewTypeErrorFrom(from string, value interface{}, exp ...ValueType) *TypeError {
valueStr := ""
valueType := ITypeOf(value)
switch valueType {
case ValueString:
valueStr = fmt.Sprintf(`"%v"`, value)
case ValueBool, ValueNumber:
valueStr = fmt.Sprintf("%v", value)
}
return &TypeError{
From: from,
Expected: exp,
Actual: valueType,
Value: valueStr,
}
}
//------------------------------------------------------------------------------
type errFrom struct {
from Function
err error
}
func (e *errFrom) Error() string {
return fmt.Sprintf("%v: %v", e.from.Annotation(), e.err)
}
func (e *errFrom) Unwrap() error {
return e.err
}
// ErrFrom wraps an error with the annotation of a function.
func ErrFrom(err error, from Function) error {
if err == nil {
return nil
}
if tErr, isTypeErr := err.(*TypeError); isTypeErr {
if tErr.From == "" {
tErr.From = from.Annotation()
}
return err
}
if _, isTypeMismatchErr := err.(*TypeMismatch); isTypeMismatchErr {
return err
}
var fErr *errFrom
if errors.As(err, &fErr) {
return err
}
return &errFrom{from, err}
}
//------------------------------------------------------------------------------
// TypeMismatch represents an error where two values should be a comparable type
// but are not.
type TypeMismatch struct {
Lfn Function
Rfn Function
Left ValueType
Right ValueType
Operation string
}
// Error implements the standard error interface.
func (t *TypeMismatch) Error() string {
return fmt.Sprintf("cannot %v types %v (from %v) and %v (from %v)", t.Operation, t.Left, t.Lfn.Annotation(), t.Right, t.Rfn.Annotation())
}
// NewTypeMismatch creates a new type mismatch error.
func NewTypeMismatch(operation string, lfn, rfn Function, left, right interface{}) *TypeMismatch {
return &TypeMismatch{
Lfn: lfn,
Rfn: rfn,
Left: ITypeOf(left),
Right: ITypeOf(right),
Operation: operation,
}
}