-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
67 lines (54 loc) · 1.05 KB
/
errors.go
File metadata and controls
67 lines (54 loc) · 1.05 KB
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
package errors
import (
"fmt"
"runtime"
"strconv"
)
type ErrorFunc func() errors
func Wrap(old error) ErrorFunc {
f := func() errors { return New(old.Error(), "2") }
return f
}
func New(params ...string) errors {
newerr := errors{}
if len(params) > 2 {
return errors{Msg: "Ops too many params", calldepth: 0}
}
for idx, value := range params {
switch {
case idx == 0:
newerr.Msg = value
newerr.calldepth = 1
case idx == 1:
depth, e := strconv.Atoi(value)
if e != nil || depth < 0 {
newerr.calldepth = 1
}
newerr.calldepth = depth
}
}
var (
ok bool
line int
file string
)
_, file, line, ok = runtime.Caller(newerr.calldepth)
if !ok {
file = "Ops no file name"
line = -1
}
if len(newerr.Msg) > 79 {
msg := []byte(newerr.Msg)
newerr.Msg = fmt.Sprintf("%s\n%s: %s: %v\n",
msg[:79], msg[79:], file, line)
}
newerr.Msg = fmt.Sprintf("%s: %s: %v\n", newerr.Msg, file, line)
return newerr
}
type errors struct {
Msg string
calldepth int
}
func (e errors) Error() string {
return e.Msg
}