-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_json.go
187 lines (164 loc) · 5.22 KB
/
error_json.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
package api
import (
"fmt"
"strings"
"reflect"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
)
type Semantic_error struct {
error string
err error
}
func (e *Semantic_error) Error() string {
return e.error
}
func (e *Semantic_error) Unwrap() error {
return e.err
}
func parse_unmarshal_json_error(err *json.SemanticError, b []byte, input any) error {
body_fields, serr := get_request_body_fields(b)
if serr != nil {
return serr
}
if err.Err != nil {
required_fields, serr := required_input_fields(input)
if serr != nil {
return serr
}
unknown_fields := unknown_input_fields(body_fields, required_fields)
if len(unknown_fields) != 0 {
return fmt.Errorf("Invalid fields: %s", strings.Join(unknown_fields, ", "))
}
} else {
if terr := invalid_field_type(err, b); terr != nil {
return terr
}
}
return &Semantic_error{fmt.Sprintf("JSON unmarshal error at byte offset %d", err.ByteOffset), err}
//return fmt.Errorf("JSON unmarshal error at byte offset %d", err.ByteOffset)
}
func parse_unmarshal_json_slice_error(err *json.SemanticError, b []byte, inputs any) (error, []error){
if err.Err != nil {
body_slice, serr := get_request_body_slice(b)
if serr != nil {
return serr, nil
}
elem := reflect.ValueOf(inputs).Elem()
if elem.Kind() != reflect.Slice {
return fmt.Errorf("Input must be a slice"), nil
}
input := elem.Index(0)
required_fields, serr := required_input_fields_elem(input)
if serr != nil {
return serr, nil
}
has_errors := false
errs := make([]error, len(body_slice))
in := input.Interface()
for i, b := range body_slice {
body_fields, serr := get_request_body_fields(b)
if serr != nil {
has_errors = true
errs[i] = serr
continue
}
unknown_fields := unknown_input_fields(body_fields, required_fields)
if len(unknown_fields) != 0 {
has_errors = true
errs[i] = fmt.Errorf("Invalid fields: %s", strings.Join(unknown_fields, ", "))
continue
}
if serr := json.Unmarshal(b, &in, json.RejectUnknownMembers(true)); err != nil {
has_errors = true
errs[i] = serr
continue
}
}
if has_errors {
return nil, errs
}
} else {
if terr := invalid_field_type(err, b); terr != nil {
return terr, nil
}
}
return &Semantic_error{fmt.Sprintf("JSON unmarshal error at byte offset %d", err.ByteOffset), err}, nil
//return fmt.Errorf("JSON unmarshal error at byte offset %d", err.ByteOffset), nil
}
func invalid_field_type(err *json.SemanticError, b []byte) *Semantic_error {
// Invalid type
switch err.JSONKind {
case 'n':
return invalid_field_type_error(err, b, "null")
case 'f', 't':
return invalid_field_type_error(err, b, "bool")
case '"':
return invalid_field_type_error(err, b, "string")
case '0':
return invalid_field_type_error(err, b, "number")
case '{', '}':
return invalid_field_type_error(err, b, "object")
case '[', ']':
return invalid_field_type_error(err, b, "array")
default:
return nil
}
}
func invalid_field_type_error(err *json.SemanticError, b []byte, kind string) *Semantic_error {
b = b[err.ByteOffset:]
n := 100
if n < len(b) {
b = append(b[:n], []byte{'.','.','.'}...)
}
return &Semantic_error{
fmt.Sprintf("Invalid JSON type. Expected '%s' but got '%s' (at byte offset %d) after: %s", err.GoType, kind, err.ByteOffset, string(b)),
err,
}
//return fmt.Errorf("Invalid JSON type. Expected '%s' but got '%s' (at byte offset %d) after: %s", err.GoType, kind, err.ByteOffset, string(b))
}
func required_input_fields(input any) (map[string]reflect.Type, *Semantic_error){
return required_input_fields_elem(reflect.ValueOf(input).Elem())
}
func required_input_fields_elem(elem reflect.Value) (map[string]reflect.Type, *Semantic_error){
list := map[string]reflect.Type{}
if elem.Kind() != reflect.Struct {
return nil, &Semantic_error{"Input must be a struct", nil}
//return list, fmt.Errorf("Input must be a struct")
}
struct_val := elem.Type()
for i := 0; i < struct_val.NumField(); i++ {
field := struct_val.Field(i)
list[strings.ToLower(field.Name)] = field.Type
}
return list, nil
}
func unknown_input_fields[K comparable, VT any, VR any](target map[K]VT, required map[K]VR) []K {
list := []K{}
for k := range target {
if _, ok := required[k]; !ok {
list = append(list, k)
}
}
return list
}
func get_request_body_fields(b []byte) (map[string]any, *Semantic_error){
var body any
if err := json.Unmarshal(b, &body); err != nil {
return nil, &Semantic_error{"Request body must be key-value pairs", err}
//return nil, fmt.Errorf("Request body must be key-value pairs")
}
fields, ok := body.(map[string]any)
if !ok {
return nil, &Semantic_error{"Request body must be key-value pairs", nil}
//return nil, fmt.Errorf("Request body must be key-value pairs")
}
return fields, nil
}
func get_request_body_slice(b []byte) (body []jsontext.Value, serr *Semantic_error){
if err := json.Unmarshal(b, &body); err != nil {
serr = &Semantic_error{"Request body must be an array", err}
//err = fmt.Errorf("Request body must be an array")
}
return
}