This repository was archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
95 lines (79 loc) · 1.56 KB
/
util.go
File metadata and controls
95 lines (79 loc) · 1.56 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
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
package xerror
import (
"errors"
"fmt"
"os"
"runtime/debug"
"github.com/pubgo/xerror/internal/utils"
"github.com/pubgo/xerror/xerror_conf"
)
func isErrNil(err error) bool { return err == nil }
func p(a ...interface{}) { _, _ = fmt.Fprintln(os.Stderr, a...) }
func handleRecover(err *error, val interface{}) {
if val == nil {
return
}
switch _val := val.(type) {
case error:
*err = _val
case string:
*err = errors.New(_val)
default:
*err = fmt.Errorf("%#v\n", _val)
}
*err = handle(*err)
}
func handle(err error, fns ...func(err *xerror)) *xerror {
err1 := &xerror{Err: err}
if _, ok := err.(XErr); !ok {
for i := 0; ; i++ {
var cc = utils.CallerWithDepth(xerror_conf.Conf.CallDepth + i)
if cc == "" {
break
}
err1.Caller = append(err1.Caller, cc)
}
} else {
err1.Caller = []string{
utils.CallerWithDepth(xerror_conf.Conf.CallDepth + 2),
}
}
if len(fns) > 0 {
fns[0](err1)
}
return err1
}
func trans(err error) *xerror {
if err == nil {
return nil
}
switch err := err.(type) {
case *xerror:
return err
case interface{ Unwrap() error }:
if err.Unwrap() == nil {
return &xerror{Detail: fmt.Sprintf("%#v", err)}
}
return &xerror{Err: err.Unwrap(), Msg: err.Unwrap().Error()}
default:
return &xerror{Msg: err.Error(), Detail: fmt.Sprintf("%#v", err)}
}
}
func printStack() {
if !xerror_conf.Conf.PrintStack {
return
}
debug.PrintStack()
}
func Cause(err error) error {
if isErrNil(err) {
return nil
}
for {
rErr := errors.Unwrap(err)
if isErrNil(rErr) {
return err
}
err = rErr
}
}