-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
42 lines (34 loc) · 840 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
40
41
42
package acp
import (
"encoding/json"
"fmt"
)
var (
_ = error(new(Error))
_ = json.Marshaler(new(Error))
_ = json.Unmarshaler(new(Error))
)
type Error struct {
Src string `json:"src,omitempty"`
Dst string `json:"dst,omitempty"`
Err error `json:"error,omitempty"`
}
type jsonError struct {
Src string `json:"src,omitempty"`
Dst string `json:"dst,omitempty"`
Err string `json:"error,omitempty"`
}
func (e *Error) Error() string {
return fmt.Sprintf("[%s => %s]: %s", e.Src, e.Dst, e.Err)
}
func (e *Error) MarshalJSON() ([]byte, error) {
return json.Marshal(&jsonError{Src: e.Src, Dst: e.Dst, Err: e.Err.Error()})
}
func (e *Error) UnmarshalJSON(buf []byte) error {
m := new(jsonError)
if err := json.Unmarshal(buf, &m); err != nil {
return err
}
e.Src, e.Dst, e.Err = m.Src, m.Dst, fmt.Errorf(m.Err)
return nil
}