|
1
|
|
|
/* |
|
2
|
|
|
Package e2h_test its the test package of the Enhanced Error Handling module |
|
3
|
|
|
*/ |
|
4
|
|
|
package e2h_test |
|
5
|
|
|
|
|
6
|
|
|
import ( |
|
7
|
|
|
"fmt" |
|
8
|
|
|
"os" |
|
9
|
|
|
"path/filepath" |
|
10
|
|
|
"runtime" |
|
11
|
|
|
|
|
12
|
|
|
"github.com/cdleo/go-commons/formatter" |
|
13
|
|
|
"github.com/cdleo/go-e2h" |
|
14
|
|
|
e2hformat "github.com/cdleo/go-e2h/formatter" |
|
15
|
|
|
) |
|
16
|
|
|
|
|
17
|
|
|
func foo() error { |
|
18
|
|
|
|
|
19
|
|
|
//Doing something, an error it's returned |
|
20
|
|
|
err := fmt.Errorf("TheError") |
|
21
|
|
|
if err != nil { |
|
22
|
|
|
//It's not mandatory, but recommended to call TraceX() from deepest possible in the stack |
|
23
|
|
|
//to get the most additional data |
|
24
|
|
|
return e2h.Trace(err) |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
//Do stuff |
|
28
|
|
|
|
|
29
|
|
|
return nil |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
func bar() error { |
|
33
|
|
|
return e2h.Tracem(foo(), "Error executing foo()") |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
func ExampleEnhancedError() { |
|
37
|
|
|
_, b, _, _ := runtime.Caller(0) |
|
38
|
|
|
hideThisPath := filepath.Dir(b) + string(os.PathSeparator) |
|
39
|
|
|
|
|
40
|
|
|
params := e2hformat.Params{ |
|
41
|
|
|
Beautify: false, |
|
42
|
|
|
InvertCallstack: true, |
|
43
|
|
|
PathHidingMethod: formatter.HidingMethod_FullBaseline, |
|
44
|
|
|
PathHidingValue: hideThisPath, |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
err := e2h.Tracef(bar(), "Error executing [%s] function", "bar()") |
|
48
|
|
|
|
|
49
|
|
|
fmt.Printf("As Error => %v\n\n", err) |
|
50
|
|
|
|
|
51
|
|
|
fmt.Printf("**** Raw Formatter ****\n\n") |
|
52
|
|
|
|
|
53
|
|
|
rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw) |
|
54
|
|
|
|
|
55
|
|
|
fmt.Printf("Just cause => %s\n\n", rawFormatter.Source(err)) |
|
56
|
|
|
|
|
57
|
|
|
fmt.Printf("Full info (inverted stack) =>\n%s\n\n", rawFormatter.Format(err, params)) |
|
58
|
|
|
|
|
59
|
|
|
params.Beautify = true |
|
60
|
|
|
fmt.Printf("Full info (beautified / inverted stack) =>\n%s\n\n", rawFormatter.Format(err, params)) |
|
61
|
|
|
|
|
62
|
|
|
fmt.Printf("**** JSON Formatter ****\n\n") |
|
63
|
|
|
|
|
64
|
|
|
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON) |
|
65
|
|
|
|
|
66
|
|
|
fmt.Printf("Just cause => %s\n\n", jsonFormatter.Source(err)) |
|
67
|
|
|
|
|
68
|
|
|
params.InvertCallstack = false |
|
69
|
|
|
params.Beautify = false |
|
70
|
|
|
fmt.Printf("Full info =>\n%s\n\n", jsonFormatter.Format(err, params)) |
|
71
|
|
|
|
|
72
|
|
|
params.Beautify = true |
|
73
|
|
|
fmt.Printf("Full info (beautified) =>\n%s\n", jsonFormatter.Format(err, params)) |
|
74
|
|
|
|
|
75
|
|
|
// Output: |
|
76
|
|
|
// As Error => TheError |
|
77
|
|
|
// |
|
78
|
|
|
// **** Raw Formatter **** |
|
79
|
|
|
// |
|
80
|
|
|
// Just cause => TheError |
|
81
|
|
|
// |
|
82
|
|
|
// Full info (inverted stack) => |
|
83
|
|
|
// github.com/cdleo/go-e2h_test.ExampleEnhancedError (e2h_example_test.go:47) [Error executing [bar()] function]; github.com/cdleo/go-e2h_test.bar (e2h_example_test.go:33) [Error executing foo()]; github.com/cdleo/go-e2h_test.foo (e2h_example_test.go:24); TheError; |
|
84
|
|
|
// |
|
85
|
|
|
// Full info (beautified / inverted stack) => |
|
86
|
|
|
// github.com/cdleo/go-e2h_test.ExampleEnhancedError (e2h_example_test.go:47) |
|
87
|
|
|
// Error executing [bar()] function |
|
88
|
|
|
// github.com/cdleo/go-e2h_test.bar (e2h_example_test.go:33) |
|
89
|
|
|
// Error executing foo() |
|
90
|
|
|
// github.com/cdleo/go-e2h_test.foo (e2h_example_test.go:24) |
|
91
|
|
|
// TheError |
|
92
|
|
|
// |
|
93
|
|
|
// **** JSON Formatter **** |
|
94
|
|
|
// |
|
95
|
|
|
// Just cause => {"error":"TheError"} |
|
96
|
|
|
// |
|
97
|
|
|
// Full info => |
|
98
|
|
|
// {"error":"TheError","stack_trace":[{"func":"github.com/cdleo/go-e2h_test.foo","caller":"e2h_example_test.go:24"},{"func":"github.com/cdleo/go-e2h_test.bar","caller":"e2h_example_test.go:33","context":"Error executing foo()"},{"func":"github.com/cdleo/go-e2h_test.ExampleEnhancedError","caller":"e2h_example_test.go:47","context":"Error executing [bar()] function"}]} |
|
99
|
|
|
// |
|
100
|
|
|
// Full info (beautified) => |
|
101
|
|
|
// { |
|
102
|
|
|
// "error": "TheError", |
|
103
|
|
|
// "stack_trace": [ |
|
104
|
|
|
// { |
|
105
|
|
|
// "func": "github.com/cdleo/go-e2h_test.foo", |
|
106
|
|
|
// "caller": "e2h_example_test.go:24" |
|
107
|
|
|
// }, |
|
108
|
|
|
// { |
|
109
|
|
|
// "func": "github.com/cdleo/go-e2h_test.bar", |
|
110
|
|
|
// "caller": "e2h_example_test.go:33", |
|
111
|
|
|
// "context": "Error executing foo()" |
|
112
|
|
|
// }, |
|
113
|
|
|
// { |
|
114
|
|
|
// "func": "github.com/cdleo/go-e2h_test.ExampleEnhancedError", |
|
115
|
|
|
// "caller": "e2h_example_test.go:47", |
|
116
|
|
|
// "context": "Error executing [bar()] function" |
|
117
|
|
|
// } |
|
118
|
|
|
// ] |
|
119
|
|
|
// } |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|