|
28 | 28 | ErrNotAssignable = errors.New("jsonpointer: invalid value type") |
29 | 29 | // ErrNotFound indicates a JSONPointer is not reachable from the root object |
30 | 30 | // (e.g. a nil pointer, missing map key). |
31 | | - ErrNotFound = errors.New(`jsonpointer: token path not found`) |
32 | | - // ErrOutOfBounds indicates an index is out of bounds for an array or slice |
33 | | - ErrOutOfBounds = errors.New("jsonpointer: index out of bounds") |
| 31 | + ErrNotFound = errors.New(`jsonpointer: value not found`) |
| 32 | + // ErrOutOfRange indicates an index is out of range for an array or slice |
| 33 | + ErrOutOfRange = errors.New("jsonpointer: index out of range") |
34 | 34 | // ErrInvalidReference indicates a reference is not reachable. This occurs |
35 | 35 | // when a primitive leaf node is reached and the reference is not empty. |
36 | 36 | ErrInvalidReference = errors.New("jsonpointer: bad reference") |
@@ -72,14 +72,24 @@ type ptrError struct { |
72 | 72 | } |
73 | 73 |
|
74 | 74 | func (e *ptrError) Error() string { |
75 | | - return e.err.Error() |
| 75 | + t, ok := e.Token() |
| 76 | + fmt.Println("e.current", e.current) |
| 77 | + fmt.Println("TOKEN ERROR", t, ok) |
| 78 | + if ok { |
| 79 | + return fmt.Sprintf(`"%v for token "%s" in reference "%v"`, e.err.Error(), t, e.ptr) |
| 80 | + } |
| 81 | + return fmt.Sprintf(`"%v" for reference "%v"`, e.err.Error(), e.ptr) |
76 | 82 | } |
77 | 83 |
|
78 | 84 | // Unwrap returns the underlying error. |
79 | 85 | func (e *ptrError) Unwrap() error { |
80 | 86 | return e.err |
81 | 87 | } |
82 | 88 |
|
| 89 | +func (e *ptrError) updateState(s *state) { |
| 90 | + e.state = *s |
| 91 | +} |
| 92 | + |
83 | 93 | // JSONPointer returns the initial JSONPointer. |
84 | 94 | func (e *ptrError) JSONPointer() JSONPointer { |
85 | 95 | return e.ptr |
@@ -217,3 +227,60 @@ func (e *valueError) Error() string { |
217 | 227 | func (e *valueError) ValueType() reflect.Type { |
218 | 228 | return e.valuetype |
219 | 229 | } |
| 230 | + |
| 231 | +func updateErrorState(err error, s *state) { |
| 232 | + if e, ok := err.(interface{ updateState(s *state) }); ok { |
| 233 | + e.updateState(s) |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +// IndexError indicates an error occurred with regards to an index of a slice or |
| 238 | +// array. The error may be wrapped in an Error if it is returned from an operation on a |
| 239 | +// JSON Pointer. |
| 240 | +// |
| 241 | +// err.Index() will return -1 if: |
| 242 | +// |
| 243 | +// - the source or destination is an array, token is equal to "-", |
| 244 | +// and the array does not have a zero value. |
| 245 | +// |
| 246 | +// - the token can not be parsed as an int |
| 247 | +// |
| 248 | +type IndexError interface { |
| 249 | + MaxIndex() int |
| 250 | + Index() int |
| 251 | + Error() string |
| 252 | + Unwrap() error |
| 253 | +} |
| 254 | + |
| 255 | +type indexError struct { |
| 256 | + err error |
| 257 | + maxIndex int |
| 258 | + index int |
| 259 | +} |
| 260 | + |
| 261 | +func (e *indexError) MaxIndex() int { |
| 262 | + return e.maxIndex |
| 263 | +} |
| 264 | + |
| 265 | +func (e *indexError) Index() int { |
| 266 | + return e.index |
| 267 | +} |
| 268 | + |
| 269 | +func (e *indexError) Error() string { |
| 270 | + if errors.Is(e.err, ErrOutOfRange) { |
| 271 | + return fmt.Sprintf("%v; expected index to be less than next (%d) but is (%d)", ErrOutOfRange, e.maxIndex, e.index) |
| 272 | + } |
| 273 | + return fmt.Sprintf("%v for index %d of %d", e.err.Error(), e.index, e.maxIndex) |
| 274 | +} |
| 275 | + |
| 276 | +// AsIndexError is a convenience function which calls calls errors.As, returning |
| 277 | +// err as an IndexError and true or nil and false if err can not be assigned to |
| 278 | +// an IndexError |
| 279 | +func (e *indexError) AsIndexError(err error) (IndexError, bool) { |
| 280 | + var ie IndexError |
| 281 | + return ie, errors.As(err, &ie) |
| 282 | +} |
| 283 | + |
| 284 | +func (e *indexError) Unwrap() error { |
| 285 | + return e.err |
| 286 | +} |
0 commit comments