-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_test.go
78 lines (61 loc) · 2.33 KB
/
output_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
package uhoh
import (
"errors"
"fmt"
"testing"
"time"
)
func ExampleSetDefaultErrorFormatter() {
// Set original formatter to be used to reset it later
originalFormatter := defaultErrorFormat
SetDefaultErrorFormatter(func(err *Err) string {
return err.Original.Error()
})
originalErr := errors.New("original error")
describeErr := errors.New("describe error")
err := New(originalErr).SetDescribe(describeErr).SetType(ErrGeneral)
fmt.Printf("%s", err.Error())
// Set the default error formatter back to the default
SetDefaultErrorFormatter(originalFormatter)
// Output:
// original error
}
func ExampleErr_Error() {
originalErr := errors.New("original error")
describeErr := errors.New("describe error")
err := New(originalErr).SetDescribe(describeErr).SetType(ErrGeneral)
err.SetDate(time.Date(2021, time.Month(9), 12, 1, 10, 30, 0, time.UTC))
fmt.Printf("%s", err.Error())
// Output:
// 2021-09-12T01:10:30Z | general error | original error | describe error
}
func TestErr_Error(t *testing.T) {
originalErr := errors.New("original error")
describeErr := errors.New("describe error")
err := New(originalErr).SetDescribe(describeErr).SetType(ErrGeneral)
err.SetDate(time.Date(2021, time.Month(9), 12, 1, 10, 30, 0, time.UTC))
if err.Error() != "2021-09-12T01:10:30Z | general error | original error | describe error" {
t.Errorf("Error() = %v, want %v", err.Error(), "2021-09-12T01:10:30Z | general error | original error | describe error")
}
}
func TestErr_ErrorNil(t *testing.T) {
var err *Err
if err.Error() != "" {
t.Errorf("Error() = %v, want %v", err.Error(), "")
}
}
func ExampleErr_ToJSON() {
originalErr := errors.New("original error")
describeErr := errors.New("describe error")
err := New(originalErr).SetDescribe(describeErr).SetType(ErrGeneral)
err.SetDate(time.Date(2021, time.Month(9), 12, 1, 10, 30, 0, time.UTC))
fmt.Printf("%s", err.ToJSON())
// Output:
// {"date":"2021-09-12T01:10:30Z","describe":"describe error","original":"original error","stack":[{"file":"output_test.go","function":"ExampleErr_ToJSON","line":65},{"file":"run_example.go","function":"runExample","line":64},{"file":"example.go","function":"runExamples","line":44}],"type":"general error"}
}
func TestToMapStrNil(t *testing.T) {
var err *Err
if err.ToMapStr() != nil {
t.Errorf("ToMapStr() = %v, want %v", err.ToMapStr(), nil)
}
}