-
Notifications
You must be signed in to change notification settings - Fork 4
/
jerr.go
154 lines (135 loc) · 2.99 KB
/
jerr.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package jerr
import (
"encoding/json"
"fmt"
"github.com/jchavannes/jgo/jutil"
"os"
"runtime/debug"
"strings"
"time"
)
type JError struct {
Messages []string
}
const (
boldStart = "\x1b[1m"
formatEnd = "\x1b[0m"
colorDefault = "\x1b[39m"
colorRed = "\x1b[31m"
colorYellow = "\x1b[33m"
)
func (e JError) Error() string {
return e.getText(false)
}
func (e JError) Print() {
fmt.Println(e.getText(false))
}
func (e JError) PrintWithStack() {
Getf(e, "Stack:\n%s", debug.Stack()).Print()
}
func (e JError) Warn() {
fmt.Println(e.getText(true))
}
func (e JError) Fatal() {
e.Print()
os.Exit(1)
}
func (e JError) Panic() {
panic(e)
}
func (e JError) getText(warn bool) string {
returnString := ""
for i := len(e.Messages) - 1; i >= 0; i-- {
returnString += "\n " + boldStart + "[" + fmt.Sprintf("%d", len(e.Messages)-i) + "]" + formatEnd + " " + e.Messages[i]
}
timeStr := time.Now().Format("2006-01-02 15:04:05")
if warn {
return boldStart + colorYellow + "Warning at" + timeStr + ":" + colorDefault + formatEnd + returnString
} else {
return boldStart + colorRed + "Error at " + timeStr + ":" + colorDefault + formatEnd + returnString
}
}
func (e JError) JSON() string {
data, _ := json.Marshal(jutil.ReverseStringSlice(e.Messages))
return string(data)
}
func Get(message string, err error) JError {
if err == nil {
return Get(message, New("nil error!"))
}
returnError := Create(err)
returnError.Messages = append(returnError.Messages, message)
return returnError
}
func Create(err error) JError {
if err == nil {
return Create(New("nil error!"))
}
var returnError JError
switch t := err.(type) {
case JError:
returnError = t
default:
returnError = JError{
Messages: []string{err.Error()},
}
}
return returnError
}
func Getf(err error, format string, a ...interface{}) JError {
return Get(fmt.Sprintf(format, a...), err)
}
func New(message string) JError {
return JError{
Messages: []string{message},
}
}
func Newf(format string, a ...interface{}) JError {
return New(fmt.Sprintf(format, a...))
}
func Combine(errorArray ...error) JError {
var returnError JError
for _, err := range errorArray {
if jutil.IsNil(err) {
returnError.Messages = append(returnError.Messages, fmt.Sprintf("error nil for combine: %T", err))
continue
}
switch t := err.(type) {
case JError:
returnError.Messages = append(returnError.Messages, t.Messages...)
default:
returnError.Messages = append(returnError.Messages, t.Error())
}
}
return returnError
}
func HasError(e error, s string) bool {
if e == nil {
return false
}
err, ok := e.(JError)
if !ok {
return e.Error() == s
}
for _, errMessage := range err.Messages {
if errMessage == s {
return true
}
}
return false
}
func HasErrorPart(e error, s string) bool {
if e == nil {
return false
}
err, ok := e.(JError)
if !ok {
return strings.Contains(e.Error(), s)
}
for _, errMessage := range err.Messages {
if strings.Contains(errMessage, s) {
return true
}
}
return false
}