-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherr.go
82 lines (69 loc) · 1.72 KB
/
err.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
package errstack
import (
"fmt"
"os"
"runtime"
"strings"
"sync"
)
// Err struct contains filename, line number, and error message
type Err struct {
showMetadata bool
trimFilename bool
filename string
line int
err string
mtx *sync.Mutex
}
// NewErr returns *Err struct
func NewErr(err string) *Err {
_, filename, line, _ := runtime.Caller(2)
e := &Err{
filename: filename,
line: line,
err: err,
showMetadata: true,
trimFilename: false,
mtx: &sync.Mutex{},
}
return e
}
// getFilename is helper function that returns shorten or original filename
func (e *Err) getFilename() string {
if e.trimFilename {
chunks := strings.Split(e.filename, string(os.PathSeparator))
chunksLength := len(chunks)
domainIndex := -1
for i, chunk := range chunks {
if strings.HasSuffix(chunk, ".com") || strings.HasSuffix(chunk, ".edu") || strings.HasSuffix(chunk, ".org") || strings.HasSuffix(chunk, ".net") || strings.HasSuffix(chunk, ".io") {
domainIndex = i
break
}
}
if domainIndex >= 0 {
return strings.Join(chunks[domainIndex:chunksLength], string(os.PathSeparator))
}
}
return e.filename
}
// SetShowMetadata is a flag to display/hide filename and line number
func (e *Err) SetShowMetadata(showMetadata bool) *Err {
e.mtx.Lock()
e.showMetadata = showMetadata
e.mtx.Unlock()
return e
}
// SetTrimFilename is a flag to trim filename
func (e *Err) SetTrimFilename(trimFilename bool) *Err {
e.mtx.Lock()
e.trimFilename = trimFilename
e.mtx.Unlock()
return e
}
// Error satisfies the error interface
func (e *Err) Error() string {
if e.showMetadata {
return fmt.Sprintf(`%s:%d="%v"`, e.getFilename(), e.line, e.err)
}
return e.err
}