-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleError_test.go
52 lines (39 loc) · 1.23 KB
/
singleError_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
package derp
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestError(t *testing.T) {
e := New(CodeInternalError, "Location", "Message")
require.Equal(t, "Location: Message", e.Error())
require.Equal(t, CodeInternalError, ErrorCode(e))
WithNotFound()(&e)
require.Equal(t, 404, e.GetErrorCode())
require.Equal(t, 404, ErrorCode(e))
require.True(t, NotFound(e))
}
func TestError_WrapSingle(t *testing.T) {
inner := New(101, "A", "B", "C")
outer := Wrap(inner, "C", "D").(Error)
require.Equal(t, outer.Code, 101)
require.Equal(t, outer.Location, "C")
require.Equal(t, outer.Message, "D")
innerAgain := outer.Unwrap().(Error)
require.Equal(t, innerAgain.Code, 101)
require.Equal(t, innerAgain.Location, "A")
require.Equal(t, innerAgain.Message, "B")
}
func TestError_WrapGeneric(t *testing.T) {
inner := errors.New("omg it works")
outer := Wrap(inner, "C", "D").(Error)
require.Equal(t, outer.Code, 500)
require.Equal(t, outer.Location, "C")
require.Equal(t, outer.Message, "D")
innerAgain := outer.Unwrap()
require.Equal(t, innerAgain.Error(), "omg it works")
}
func TestErrorCodeSetter(t *testing.T) {
err := NewInternalError("test", "test", WithNotFound())
require.Equal(t, 404, ErrorCode(err))
}