-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherr_json.go
More file actions
30 lines (27 loc) · 807 Bytes
/
Copy patherr_json.go
File metadata and controls
30 lines (27 loc) · 807 Bytes
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
package errors
// JSONFunc converts the Error instance to a JSON value. It is
// recommended to return one of the following:
//
// - map[string]interface{}
// - []map[string]inteface{}
// - CustomType
//
// In certain cases, such as validation errors, it is reasonable to
// expand a single error instance into multiple 'Objects'.
//
// Care must be taken not to expose details internal to the application.
// This is for two reasons, namely security and user experience.
type JSONFunc func(*Error) interface{}
// JSON is the default implementation of representing the error as a
// JSON value.
func (e *Error) JSON() interface{} {
if e.ToJSON != nil {
return e.ToJSON(e)
}
k := WhatKind(e)
return map[string]interface{}{
"code": k.Code,
"error": k.String(),
"msg": UserMsg(e),
}
}