-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
78 lines (69 loc) · 1.92 KB
/
errors_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 errors
import (
sysErrors "errors"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNew(t *testing.T) {
err := New("error1")
assert.Equal(t, "error1", err.Error())
}
func TestNewF(t *testing.T) {
err := NewF("error%d = %s", 1, "failure")
assert.Equal(t, "error1 = failure", err.Error())
}
func TestAdd(t *testing.T) {
err := New("error1").Add("error2").Add("error3")
assert.Equal(t, "error3\nerror2\nerror1", err.Error())
}
func TestOr(t *testing.T) {
var err1 error
err2 := sysErrors.New("error2")
err3 := Or(err1, err2)
assert.Equal(t, "error2", err3.Error())
}
func TestFrom(t *testing.T) {
f := func() error { return sysErrors.New("error during 'f()'") }
f2 := func() error { return From(f()).Add("error during 'f2()'") }
e := f2()
assert.Equal(t, "error during 'f2()'\nerror during 'f()'", e.Error())
}
func TestFromTuple(t *testing.T) {
f := func() (int, error) { return 5, sysErrors.New("error during 'f()'") }
f2 := func() (interface{}, error) { return GetFromTupleAdd("error during 'f2()'")(f()) }
v, e := f2()
assert.Equal(t, "error during 'f2()'\nerror during 'f()'", e.Error())
assert.Equal(t, 5, v.(int))
}
func TestExample(t *testing.T) {
mockApiFunc := func() error {
return sysErrors.New("unexpected column found")
}
modelFunc := func() *Error {
return From(mockApiFunc()).Add("failed to upsert value")
}
businessFunc := func() *Error {
return modelFunc().AddF("failed to perform daily sync of customer '%d'", 35)
}
// consume
handlerFunc := func() {
if err := businessFunc(); err != nil {
fmt.Println("Error handling request")
fmt.Println("Error: ")
fmt.Println(err)
fmt.Println()
fmt.Println("Cause:")
fmt.Println(err.Cause())
fmt.Println()
fmt.Println("Symptom:")
fmt.Println(err.Symptom())
}
}
handlerFunc()
// return as built-in Error, e.g. to AWS API Gateway
extHandlerFunc := func() error {
return businessFunc()
}
_ = extHandlerFunc()
}