-
Notifications
You must be signed in to change notification settings - Fork 6
/
error.go
49 lines (41 loc) · 1.06 KB
/
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
43
44
45
46
47
48
49
package output
import (
"encoding/json"
)
// Error implements output.Command for a return value that is an error
type Error struct {
Value error
Resolved string
Original string
}
// MarshalJSON implements json.Marshaler and output.Output
func (e Error) MarshalJSON() ([]byte, error) {
marshaled, err := e.MarshalRawMap()
if err != nil {
return json.MarshalIndent(map[string]interface{}{
"error": err.Error(),
}, "", " ")
}
return json.MarshalIndent(marshaled, "", " ")
}
// MarshalHuman implements output.Output
func (e Error) MarshalHuman() ([]byte, error) {
// Human readable errors go to stderr. This output package is responsible for stdout output only.
return []byte(""), nil
}
// MarshalRawMap implements output.Output
func (e Error) MarshalRawMap() (map[string]interface{}, error) {
m := make(map[string]interface{})
if e.Resolved != "" {
m["resource"] = e.Resolved
}
if e.Original != "" {
m["argument"] = e.Original
}
if e.Value == nil {
m["error"] = "Unspecified error"
} else {
m["error"] = e.Value.Error()
}
return m, nil
}