-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_error_test.go
70 lines (63 loc) · 1.81 KB
/
example_error_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
package gdk_test
import (
"encoding/json"
"fmt"
"github.com/researchlab/gdk"
)
func jsonMarshal(size int) error {
_, err := json.Marshal(make(chan struct{}, size))
if err != nil {
return gdk.ErrorCause(err)
}
return nil
}
// deal with the given error
func ExampleErrorCause() {
const ERR_MARSHAL_FAILED = 1000
var size = 10
var e gdk.Err
err := jsonMarshal(size)
if err != nil {
e = gdk.ErrorCause(err).WithCode(ERR_MARSHAL_FAILED).WithTag("ParseError").
WithFields(map[string]interface{}{
"inputs": map[string]int{
"size": 10,
},
})
}
if e != nil {
// e not equal nil
}
errTxt := e.DetailText()
fmt.Println(errTxt)
// Output:
// CallChains=ExampleErrorCause.jsonMarshal, Tag=ParseError, Fields={"inputs":{"size":10}}, Code=1000, Error=json: unsupported type: chan struct {}
}
// new error with format
func ExampleErrorf() {
const MIN_VALUE = 5
const ERR_PARAMS_INVALID = "ERR PARAMS INVALID"
err := gdk.Errorf("params invalid, size need > %d", MIN_VALUE).WithCode(ERR_PARAMS_INVALID)
fmt.Println(err.DetailText())
//Output:
// CallChains=ExampleErrorf, Code=ERR PARAMS INVALID, Error=params invalid, size need > 5
}
// use error Templates
func ExampleErrorT() {
const (
ERR_PARAMS_INVALID = "PARAMS INVALID"
)
var errorTemplates = map[any]string{
ERR_PARAMS_INVALID: "params invalid, include(%v)",
}
gdk.SetGlobalErrorTemplates(errorTemplates)
gdk.SetGlobalTag("ip:192.168.190.70")
gdk.SetGlobalFields(map[string]interface{}{
"service": "timer-job-3",
})
defer gdk.UnsetGlobals()
err := gdk.ErrorT(ERR_PARAMS_INVALID, "size need > 5, code need > 0")
fmt.Println(err.DetailText())
// Output:
// CallChains=ExampleErrorT, GlobalTag=ip:192.168.190.70, GlobalFields={"service":"timer-job-3"}, Code=PARAMS INVALID, Error=params invalid, include(size need > 5, code need > 0)
}