-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
124 lines (98 loc) Β· 2.78 KB
/
format.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
package terrors
import (
"fmt"
"strings"
"github.com/fatih/color"
)
func FileNameOfPath(path string) string {
tot := strings.Split(path, "/")
if len(tot) > 1 {
return tot[len(tot)-1]
}
return path
}
func FormatCallerFromFrame(frm Frame) string {
pkg, _, filestr, linestr := frm.Location()
return FormatCaller(pkg, filestr, linestr)
}
func FormatCaller(pkg, path string, number int) string {
pkgd := ColorBrackets("pkg", color.New(color.FgHiGreen).Sprint(pkg))
pathd := ColorBrackets("file", fmt.Sprintf("%s:%s", color.New(color.Bold).Sprint(FileNameOfPath(path)), color.New(color.FgHiRed, color.Bold).Sprintf("%d", number)))
return fmt.Sprintf("%s%s", pkgd, pathd)
}
func ColorBrackets(label string, value string) string {
closeBracket := color.New(color.Faint, color.FgHiCyan).Sprint("]")
openBracket := color.New(color.Faint, color.FgHiCyan).Sprint("[")
return fmt.Sprintf("%s%s%s%s%s", openBracket, color.New(color.Faint, color.FgHiMagenta).Sprint(label), color.New(color.Faint, color.FgBlack).Sprint("="), value, closeBracket)
}
func ColorCode(code int) string {
openBracket := color.New(color.Faint, color.FgHiRed).Sprint("{")
closeBracket := color.New(color.Faint, color.FgHiRed).Sprint("}")
return fmt.Sprintf("%s%s%s%s%s", openBracket, color.New(color.Faint, color.FgHiBlack).Sprint("code"), color.New(color.Faint, color.FgBlack).Sprint("="), color.New(color.FgHiRed, color.Bold).Sprint(code), closeBracket)
}
func ExtractErrorDetail(err error) string {
if frm, ok := Cause2(err); ok {
return frm.Detail()
}
return "no error detail found"
}
func FormatErrorCaller(err error, name string, verbose bool) string {
// caller := ""
dets := ""
var errstr string
if frm, ok := Cause2(err); ok {
if verbose {
errstr = frm.Simple()
dets = frm.Detail()
} else {
errstr = frm.Error()
}
} else {
errstr = err.Error()
}
if verbose {
if dets != "" {
dets = fmt.Sprintf("\n\n%s\n", dets)
}
} else {
dets = ""
}
if name != "" {
name = "[" + name + "] - "
}
return fmt.Sprintf("%s%s%s", name, color.New(color.FgRed).Sprint(errstr), dets)
}
func InlineChainFormatter(self func() string, kid error) string {
if kid == nil {
slf := self()
if !strings.Contains(slf, "β") {
return "β " + slf
}
return slf
}
errd := kid.Error()
arrow := "π"
if !strings.Contains(errd, arrow) && !strings.HasPrefix(errd, "β") {
arrow += " β"
}
return fmt.Sprintf("%s %s %s", self(), arrow, errd)
}
func FullChainFormatter(kid error) string {
chain := GetChain(kid)
wrk := "\n\n"
for i, err := range chain {
arrow := "π"
if len(chain)-1 == i {
arrow = "β"
}
wrk += arrow + " "
switch v := err.(type) {
case *wrapError:
wrk += v.DetailedSelf()
default:
wrk += fmt.Sprintf("%s\n\n", v.Error())
}
}
wrk += "\n\n"
return wrk
}