-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2hTracer.go
57 lines (48 loc) · 1.23 KB
/
e2hTracer.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
/*
Package e2h its the package of the Enhanced Error Handling module
*/
package e2h
import (
"fmt"
"runtime"
)
// This function calls the addTrace in order to create or add stack info
func Trace(e error) error {
return addTrace(e, "")
}
// Same as Trace, but adding a descriptive message
func Tracem(e error, message string) error {
return addTrace(e, message)
}
// Same as Tracem, but the descriptive message can have formatted values
func Tracef(e error, format string, args ...interface{}) error {
return addTrace(e, format, args...)
}
// This is the private function that creates the first EnhancedError
// with info or add the new info to the existing one
func addTrace(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
message := format
if args != nil {
message = fmt.Sprintf(format, args...)
}
pc, file, line, _ := runtime.Caller(2)
info := StackDetails{
File: file,
Line: line,
FuncName: runtime.FuncForPC(pc).Name(),
Message: message,
}
switch err.(type) {
case EnhancedError:
err.(*enhancedError).stack = append(err.(*enhancedError).stack, info)
return err
default:
return &enhancedError{
err: err,
stack: append(make([]StackDetails, 0), info),
}
}
}