/
errors.go
61 lines (57 loc) · 2.12 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
package jsonapivalidator
func validateErrors(e interface{}, result *Result) {
errors, ok := e.([]interface{})
if !ok {
result.AddError(ErrInvalidErrorsType)
return // cannot procceed
}
// Validate each element is an error object
for _, err := range errors {
validateErrorObject(err, result)
}
}
// errors map[string]interface{}, result *Result
func validateErrorObject(e interface{}, result *Result) {
// The value of the attributes key MUST be an object (an “attributes object”)
err, ok := e.(map[string]interface{})
if !ok {
result.AddError(ErrNotErrorObject)
return // cannot procceed
}
for k, v := range err {
switch k {
case memberID:
// a unique identifier for this particular occurrence of the problem
case memberLinks:
// a links object containing the following members:
// about: a link that leads to further details about this particular
// occurrence of the problem.
validateLinksObject(v, result, nil)
case memberStatus:
// the HTTP status code applicable to this problem, expressed as a string
// value.
case memberCode:
// an application-specific error code, expressed as a string value
case memberTitle:
// a short, human-readable summary of the problem that SHOULD NOT change
// from occurrence to occurrence of the problem, except for purposes of
// localization.
case memberDetail:
// a human-readable explanation specific to this occurrence of the
// problem. Like title, this field’s value can be localized
case memberSource:
// an object containing references to the source of the error, optionally
// including any of the following members:
// pointer: a JSON Pointer [RFC6901] to the associated entity in the
// request document [e.g. "/data" for a primary data object, or
// "/data/attributes/title" for a specific attribute]
// parameter: a string indicating which URI query parameter caused the
// error
case memberMeta:
// a meta object containing non-standard meta-information about the error
validateMetaObject(v, result)
default:
result.AddError(ErrInvalidErrorMember)
}
}
}