-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patherrors.go
131 lines (112 loc) · 3.26 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
package jmap
import (
"encoding/json"
"errors"
)
// The RequestError structure is "problem details" object as defined by
// RFC 7807. Any fields except for Type can be empty.
//
// Used for all problem types that don't have any custom fields. Currently:
// - urn:ietf:params:jmap:error:unknownCapability (ProblemUnknownCapability)
// - urn:ietf:params:jmap:error:notJSON (ProblemNotJSON)
// - urn:ietf:params:jmap:error:notRequest (ProblemNotRequest)
type RequestError struct {
// A URI reference that identifies the problem type.
Type ErrorCode `json:"type"`
// A short, human-readable summary of the problem type.
Title string `json:"title,omitempty"`
// The HTTP status code.
Status int `json:"status,omitempty"`
// A human-readable explanation specific to this occurrence of the problem.
Detail string `json:"detail,omitempty"`
// A URI reference that identifies the specific occurrence of the problem.
Instance string `json:"instance,omitempty"`
// All other fields.
Properties map[string]interface{} `json:"-"`
}
func (re RequestError) Error() string {
return re.Detail
}
type requestError RequestError
func (re *RequestError) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, (*requestError)(re)); err != nil {
return err
}
if err := json.Unmarshal(data, &re.Properties); err != nil {
return err
}
if _, ok := re.Properties["type"]; !ok {
return errors.New("jmap: missing type field in error object")
}
delete(re.Properties, "type")
delete(re.Properties, "title")
delete(re.Properties, "status")
delete(re.Properties, "detail")
delete(re.Properties, "instance")
if len(re.Properties) == 0 {
re.Properties = nil
}
return nil
}
func (re RequestError) MarshalJSON() ([]byte, error) {
allProps := make(map[string]interface{}, len(re.Properties)+5)
for k, v := range re.Properties {
allProps[k] = v
}
if re.Type != "" {
allProps["type"] = re.Type
}
if re.Title != "" {
allProps["title"] = re.Title
}
if re.Status != 0 {
allProps["status"] = re.Status
}
if re.Detail != "" {
allProps["detail"] = re.Detail
}
if re.Instance != "" {
allProps["instance"] = re.Instance
}
return json.Marshal(allProps)
}
// The MethodError structure describes method-level error.
//
// See section 3.5.2 of JMAP Core specification.
type MethodErrorArgs struct {
Type ErrorCode `json:"type"`
// All fields other than Type.
Properties map[string]interface{}
}
func (me MethodErrorArgs) Error() string {
return "jmap: " + string(me.Type)
}
func (me MethodErrorArgs) MarshalJSON() ([]byte, error) {
fullProps := make(map[string]interface{}, len(me.Properties)+1)
for k, v := range me.Properties {
fullProps[k] = v
}
fullProps["type"] = me.Type
return json.Marshal(fullProps)
}
func (me *MethodErrorArgs) UnmarshalJSONArgs(data []byte) error {
if err := json.Unmarshal(data, &me); err != nil {
return err
}
if err := json.Unmarshal(data, &me.Properties); err != nil {
return err
}
if _, ok := me.Properties["type"]; !ok {
return errors.New("jmap: missing type field in error object")
}
delete(me.Properties, "type")
if len(me.Properties) == 0 {
me.Properties = nil
}
return nil
}
func UnmarshalMethodErrorArgs(args json.RawMessage) (interface{}, error) {
res := MethodErrorArgs{}
err := res.UnmarshalJSONArgs(args)
return res, err
}