-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2h_example_test.go
121 lines (98 loc) · 3.51 KB
/
e2h_example_test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Package e2h_test its the test package of the Enhanced Error Handling module
*/
package e2h_test
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/cdleo/go-commons/formatter"
"github.com/cdleo/go-e2h"
e2hformat "github.com/cdleo/go-e2h/formatter"
)
func foo() error {
//Doing something, an error it's returned
err := fmt.Errorf("TheError")
if err != nil {
//It's not mandatory, but recommended to call TraceX() from deepest possible in the stack
//to get the most additional data
return e2h.Trace(err)
}
//Do stuff
return nil
}
func bar() error {
return e2h.Tracem(foo(), "Error executing foo()")
}
func ExampleEnhancedError() {
_, b, _, _ := runtime.Caller(0)
hideThisPath := filepath.Dir(b) + string(os.PathSeparator)
params := e2hformat.Params{
Beautify: false,
InvertCallstack: true,
PathHidingMethod: formatter.HidingMethod_FullBaseline,
PathHidingValue: hideThisPath,
}
err := e2h.Tracef(bar(), "Error executing [%s] function", "bar()")
fmt.Printf("As Error => %v\n\n", err)
fmt.Printf("**** Raw Formatter ****\n\n")
rawFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_Raw)
fmt.Printf("Just cause => %s\n\n", rawFormatter.Source(err))
fmt.Printf("Full info (inverted stack) =>\n%s\n\n", rawFormatter.Format(err, params))
params.Beautify = true
fmt.Printf("Full info (beautified / inverted stack) =>\n%s\n\n", rawFormatter.Format(err, params))
fmt.Printf("**** JSON Formatter ****\n\n")
jsonFormatter, _ := e2hformat.NewFormatter(e2hformat.Format_JSON)
fmt.Printf("Just cause => %s\n\n", jsonFormatter.Source(err))
params.InvertCallstack = false
params.Beautify = false
fmt.Printf("Full info =>\n%s\n\n", jsonFormatter.Format(err, params))
params.Beautify = true
fmt.Printf("Full info (beautified) =>\n%s\n", jsonFormatter.Format(err, params))
// Output:
// As Error => TheError
//
// **** Raw Formatter ****
//
// Just cause => TheError
//
// Full info (inverted stack) =>
// 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;
//
// Full info (beautified / inverted stack) =>
// 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
//
// **** JSON Formatter ****
//
// Just cause => {"error":"TheError"}
//
// Full info =>
// {"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"}]}
//
// Full info (beautified) =>
// {
// "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"
// }
// ]
// }
}