-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
60 lines (50 loc) · 1.17 KB
/
errors.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
package errors
import (
"encoding/json"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type Error struct {
Message string `json:"message"`
Err string `json:"error"`
Config map[string]interface{} `json:"config"`
}
func New(msg string, e error) *Error {
err := &Error{
Message: msg,
Config: viper.AllSettings(),
}
if e == nil {
err.Err = ""
} else {
err.Err = e.Error()
}
return err
}
func NewErr(msg string) error {
return errors.New(msg)
}
// toPrettyJson encodes an item into a pretty (indented) JSON string
func (e *Error) String() string {
output, _ := json.MarshalIndent(e, "", " ")
return fmt.Sprintf("%s", output)
}
// toPrettyJson encodes an item into a pretty (indented) JSON string
func (e *Error) Error() string {
output, _ := json.MarshalIndent(e, "", " ")
return fmt.Sprintf("%s", output)
}
// toPrettyJson encodes an item into a pretty (indented) JSON string
func (e *Error) FailIfErr() {
if e.Err != "" {
logrus.Fatal(e.String())
}
}
// toPrettyJson encodes an item into a pretty (indented) JSON string
func (e *Error) WarnIfErr() {
if e.Err != "" {
logrus.Warn(e.String())
}
}