-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
39 lines (32 loc) · 841 Bytes
/
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
package errors
import (
"encoding/json"
"fmt"
)
// Err is the error struct used internally by the package. This type should only be used for type assertions.
type Err struct {
Message string `json:"message"`
Data Data `json:"data,omitempty"`
Stack Stack `json:"stack"`
Cause error `json:"cause,omitempty"`
}
func (e Err) Error() string {
if e.Cause != nil {
return fmt.Sprintf("%s: %s", e.Message, e.Cause.Error())
}
return e.Message
}
// Format implements fmt.Formatter. It only accepts the '+v' and 's' formats.
func (e Err) Format(s fmt.State, verb rune) {
if verb == 'v' && s.Flag('+') {
fmt.Fprintf(s, "%s", format(e, 0))
} else {
fmt.Fprintf(s, "%s", e.Error())
}
}
func (e Err) Unwrap() error {
return e.Cause
}
func (e *Err) MarshalJSON() ([]byte, error) {
return json.Marshal(toMapsSlice(e))
}