-
Notifications
You must be signed in to change notification settings - Fork 4
/
error.go
212 lines (194 loc) · 5.98 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package numgo
import (
"fmt"
"runtime"
)
type ngError struct {
s string
}
func (n *ngError) Error() string {
return n.s
}
var (
// NilError flags any error where a nil pointer is received
NilError = &ngError{"NilError: Nil pointer recieved."}
// ShapeError flags any mismatching
ShapeError = &ngError{"ShapeError: Array shapes don't match and can't be broadcast."}
// ReshapeError flags incorrect use of Reshape() calls.
// Resize() is the only call that can change the capacity of arrays.
ReshapeError = &ngError{"ReshapeError: New shape cannot change the size of the array."}
// NegativeAxis is forNew/Reshape/Resize calls with negative axis length: not allowed
NegativeAxis = &ngError{"NegativeAxis: Negative axis length received."}
// IndexError flags any attempt to index out of range
IndexError = &ngError{"IndexError: Index or Axis out of range."}
// InvIndexError flags Negative or illegal indexes
InvIndexError = &ngError{"InvIndexError: Invalid or illegal index received."}
// FoldMapError catches panics within Fold/FoldCC/Map calls.
// This will store the panic message in the debug string
// when debugging is turned off, for proper errror reporting
FoldMapError = &ngError{"FoldMapError: Fold/Map function panic encountered."}
debug bool
stackBuf []byte
)
// Debug sets the error reporting level for the library.
// To get debugging data from the library, set this to true
// and use GetDebug() in place of GetErr().
//
// Debugging information includes the function call that generated
// the error, stack trace at the point the error was generated,
// and the values involved in that function call.
// This will add overhead to error reporting and handling, so
// use it for development and debugging purposes.
func Debug(set bool) bool {
debug = set
if debug && len(stackBuf) == 0 {
stackBuf = make([]byte, 4096)
}
return debug
}
// HasErr tests for the existence of an error on the Array64 object.
//
// Errors will be maintained through a chain of function calls,
// so only the first error will be returned when GetErr() is called.
// Use HasErr() as a gate for the GetErr() or GetDebug() choice in
// error handling code.
func (a *Array64) HasErr() bool {
if a == nil || (a.data == nil && a.err == nil) {
return true
}
return a.err != nil
}
// GetErr returns the error object and clears the error from the array.
//
// This will only return an error value once per error instance. Do not use
// it in the if statement to test for the existence of an error. HasErr() is
// provided for that purpose.
func (a *Array64) GetErr() (err error) {
if a == nil || (a.data == nil && a.err == nil) {
return NilError
}
err = a.err
a.err, a.debug, a.stack = nil, "", ""
return
}
func (a *Array64) getErr() error {
if a == nil || (a.data == nil && a.err == nil) {
return NilError
}
return a.err
}
// GetDebug returns and clears the error object from the array object. The returned debug string
// will include the function that generated the error and the arguments that caused it.
//
// This debug information will only be generated and returned if numgo.Debug is set to true
// before the function call that causes the error.
func (a *Array64) GetDebug() (err error, debugStr, stackTrace string) {
if a == nil || (a.data == nil && a.err == nil) {
err = NilError
if debug {
debugStr = "Nil pointer received by GetDebug(). Source array was not initialized."
stackTrace = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return
}
err, debugStr, stackTrace = a.err, a.debug, a.stack
a.err, a.debug, a.stack = nil, "", ""
return
}
// encodeErr is a supporting function for MarshalJSON
func encodeErr(err error) int8 {
if err == nil {
return 0
}
e := err.(*ngError)
switch e {
case NilError:
return 1
case ShapeError:
return 2
case ReshapeError:
return 3
case NegativeAxis:
return 4
case IndexError:
return 5
case InvIndexError:
return 6
case FoldMapError:
return 7
}
return -1
}
// decodeErr is a supporting method for UnmarshalJSON
func decodeErr(err int8) (a error) {
switch err {
case 0:
a = nil
case 1:
a = NilError
case 2:
a = ShapeError
case 3:
a = ReshapeError
case 4:
a = NegativeAxis
case 5:
a = IndexError
case 6:
a = InvIndexError
case 7:
a = FoldMapError
default:
a = &ngError{fmt.Sprintf("Unknown error Unmarshaled: %d", err)}
}
return
}
// HasErr tests for the existence of an error on the Arrayb object.
//
// Errors will be maintained through a chain of function calls,
// so only the first error will be returned when GetErr() is called.
// Use HasErr() as a gate for the GetErr() or GetDebug() choice in
// error handling code.
func (a *Arrayb) HasErr() bool {
if a == nil || (a.data == nil && a.err == nil) {
return true
}
return a.err != nil
}
// GetErr returns the error object and clears the error from the array.
//
// This will only return an error value once per error instance. Do not use
// it in the if statement to test for the existence of an error. HasErr() is
// provided for that purpose.
func (a *Arrayb) GetErr() (err error) {
if a == nil || (a.data == nil && a.err == nil) {
return NilError
}
err = a.err
a.err, a.debug = nil, ""
return
}
func (a *Arrayb) getErr() error {
if a == nil || (a.data == nil && a.err == nil) {
return NilError
}
return a.err
}
// GetDebug returns and clears the error object from the array object. The returned debug string
// will include the function that generated the error and the arguments that caused it.
//
// This debug information will only be generated and returned if numgo.Debug is set to true
// before the function call that causes the error.
func (a *Arrayb) GetDebug() (err error, debugStr, stackTrace string) {
if a == nil || (a.data == nil && a.err == nil) {
err = NilError
if debug {
debugStr = "Nil pointer received in GetDebug(). Source array was not initialized."
stackTrace = string(stackBuf[:runtime.Stack(stackBuf, false)])
}
return
}
err, debugStr, stackTrace = a.err, a.debug, a.stack
a.err, a.debug, a.stack = nil, "", ""
return
}