-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrors.go
122 lines (98 loc) · 2.71 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
// Copyright 2016-2022 Olivier Mengué. All rights reserved.
// Use of this source code is governed by the Apache 2.0 license that
// can be found in the LICENSE file.
package jsonptr
import (
"encoding/json"
"errors"
"fmt"
"strconv"
)
var (
ErrSyntax = errors.New("invalid JSON pointer")
ErrIndex = errors.New("invalid array index")
ErrProperty = errors.New("property not found")
ErrRoot = errors.New("can't go up from root")
ErrDeleteRoot = errors.New("can't delete root")
ErrUsage = errors.New("invalid use of jsonptr.UnescapeString on string with '/'")
)
type ptrError interface {
error
rebase(base string)
}
// BadPointerError signals JSON Pointer parsing errors
type BadPointerError struct {
// Ptr is the substring of the original pointer where the error occurred
BadPtr string
// Err is ErrSyntax
Err error
}
// Error implements the 'error' interface
func (e *BadPointerError) Error() string {
return strconv.Quote(e.BadPtr) + ": " + e.Err.Error()
}
// Unwrap allows to unwrap the error (see [errors.Unwrap]).
func (e *BadPointerError) Unwrap() error {
return e.Err
}
func (e *BadPointerError) rebase(base string) {
if e != nil {
e.BadPtr = base + e.BadPtr
}
}
func syntaxError(ptr string) *BadPointerError {
return &BadPointerError{ptr, ErrSyntax}
}
// PtrError signals JSON Pointer navigation errors.
type PtrError struct {
// Ptr is the substring of the original pointer where the error occurred.
Ptr string
// Err is one of ErrIndex, ErrProperty.
Err error
}
// Error implements the 'error' interface
func (e *PtrError) Error() string {
return strconv.Quote(e.Ptr) + ": " + e.Err.Error()
}
// Unwrap allows to unwrap the error (see [errors.Unwrap]).
func (e *PtrError) Unwrap() error {
return e.Err
}
func (e *PtrError) rebase(base string) {
if e != nil {
e.Ptr = base + e.Ptr
}
}
func indexError(ptr string) *PtrError {
return &PtrError{ptr, ErrIndex}
}
func propertyError(ptr string) *PtrError {
return &PtrError{ptr, ErrProperty}
}
// DocumentError signals a document that can't be processed by this library
type DocumentError struct {
Ptr string
Err error
}
// Error implements the 'error' interface.
func (e *DocumentError) Error() string {
return e.Err.Error()
}
// Unwrap allows to unwrap the error (see [errors.Unwrap]).
func (e *DocumentError) Unwrap() error {
return e.Err
}
func (e *DocumentError) rebase(base string) {
if e != nil {
e.Ptr = base + e.Ptr
}
}
func docError(ptr string, doc interface{}) *DocumentError {
return &DocumentError{ptr, fmt.Errorf("%q: not an object or array but %T", ptr, doc)}
}
func jsonError(ptr string, err error) *DocumentError {
if e, ok := err.(*json.SyntaxError); ok {
return &DocumentError{"", e}
}
return &DocumentError{ptr, err}
}