-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
71 lines (61 loc) · 1.52 KB
/
output.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package uhoh
import (
"encoding/json"
"strings"
"time"
)
// defaultErrorFormat variable function to format for the Error output
var defaultErrorFormat = func(err *Err) string {
if err == nil {
return ""
}
// Put together an array of date, type, original, and describe
var s []string
s = append(s, err.Date.Format(time.RFC3339))
if err.Type != nil {
s = append(s, err.Type.Error())
}
if err.Original != nil {
s = append(s, err.Original.Error())
}
if err.Describe != nil {
s = append(s, err.Describe.Error())
}
// Take string array and join with ": "
return strings.Join(s, " | ")
}
// SetDefaultErrorFormatter sets the defaultErrorFormatter function
func SetDefaultErrorFormatter(function func(err *Err) string) {
defaultErrorFormat = function
}
// Error will return the date, type error(if set) original error and describe error(if set)
// as a string in the default error format
func (e *Err) Error() string {
return defaultErrorFormat(e)
}
// ToJson converts the output to a json string
func (e *Err) ToJSON() []byte {
b, _ := json.Marshal(e.ToMapStr())
return b
}
// ToMapStr converts Err to a map[string]interface{}
func (e *Err) ToMapStr() map[string]interface{} {
if e == nil {
return nil
}
m := make(map[string]interface{})
if e.Type != nil {
m["type"] = e.Type.Error()
}
if e.Original != nil {
m["original"] = e.Original.Error()
}
if e.Describe != nil {
m["describe"] = e.Describe.Error()
}
if len(e.Stack) > 0 {
m["stack"] = e.Stack
}
m["date"] = e.Date.Format(time.RFC3339)
return m
}