-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_error.go
42 lines (35 loc) · 960 Bytes
/
json_error.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
package jsonerror
import (
"encoding/json"
)
// ErrorJSON contains an array of Errors.
type ErrorJSON struct {
Errors []ErrorComp `json:"errors"`
}
// ErrorComp is a error structure that follows the json spec.
type ErrorComp struct {
ID string `json:"id,omitempty"`
Status int `json:"status,omitempty"`
Code string `json:"code,omitempty"`
Title string `json:"title,omitempty"`
Detail string `json:"detail,omitempty"`
Source Source `json:"source,omitempty"`
}
// Source is represents
type Source struct {
Pointer string `json:"pointer,omitempty"`
}
// Error returns the error in string format
func (e *ErrorJSON) Error() string {
out, _ := json.Marshal(e)
return string(out)
}
// ErrorByte returns the error in an []byte
func (e *ErrorJSON) ErrorByte() []byte {
out, _ := json.Marshal(e)
return out
}
// AddError allows adding fields to the error
func (e *ErrorJSON) AddError(comp ErrorComp) {
e.Errors = append(e.Errors, comp)
}