-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
149 lines (117 loc) · 4.94 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package errors_test
import (
"fmt"
"io"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/neighborly/go-errors"
pkgErrors "github.com/pkg/errors"
)
var _ = Describe("Errors", func() {
It("creates an error", func() {
msg := "an error with a message"
err := errors.New(msg)
Expect(errors.Code(err)).To(Equal(errors.InternalError))
Expect(err.Error()).To(Equal(msg))
Expect(errors.DisplayMessage(err)).To(Equal("Internal Error"))
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
})
It("creates an NotFound error", func() {
msg := "an error with a message"
err := errors.NotFound.New(msg)
Expect(errors.Code(err)).To(Equal(errors.NotFound))
Expect(err.Error()).To(Equal(msg))
Expect(errors.DisplayMessage(err)).To(Equal("Not Found"))
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
})
It("creates an InternalError error", func() {
msg := "an error with a message"
err := errors.InternalError.New(msg)
Expect(errors.Code(err)).To(Equal(errors.InternalError))
Expect(err.Error()).To(Equal(msg))
Expect(errors.DisplayMessage(err)).To(Equal("Internal Error"))
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
})
It("creates an InvalidArgument error", func() {
msg := "an error with a message"
err := errors.InvalidArgument.New(msg)
Expect(errors.Code(err)).To(Equal(errors.InvalidArgument))
Expect(err.Error()).To(Equal(msg))
Expect(errors.DisplayMessage(err)).To(Equal("Invalid Argument"))
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
})
It("creates an Unauthenticated error", func() {
msg := "an error with a message"
err := errors.Unauthenticated.New(msg)
Expect(errors.Code(err)).To(Equal(errors.Unauthenticated))
Expect(err.Error()).To(Equal(msg))
Expect(errors.DisplayMessage(err)).To(Equal("Unauthenticated"))
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
})
It("creates an PermissionDenied error", func() {
msg := "an error with a message"
err := errors.PermissionDenied.New(msg)
Expect(errors.Code(err)).To(Equal(errors.PermissionDenied))
Expect(err.Error()).To(Equal(msg))
Expect(errors.DisplayMessage(err)).To(Equal("Permission Denied"))
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
})
It("creates an Unknown error", func() {
msg := "an error with a message"
err := errors.Unknown.New(msg)
Expect(errors.Code(err)).To(Equal(errors.Unknown))
Expect(err.Error()).To(Equal(msg))
Expect(errors.DisplayMessage(err)).To(Equal("Unknown"))
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
})
It("wraps an error", func() {
msg := "an error"
err := errors.Wrap(errors.New(msg), "some other message")
Expect(errors.Code(err)).To(Equal(errors.InternalError))
Expect(err.Error()).To(Equal("some other message: " + msg))
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
})
It("can get error cause", func() {
err := io.EOF
wrapped := errors.Wrap(err, "some other message")
Expect(wrapped.Error()).To(Equal("some other message: " + err.Error()))
Expect(errors.Cause(wrapped)).To(Equal(err))
Expect(errors.DisplayMessage(err)).To(Equal("Internal Error"))
})
It("can get error cause with multiple wraps", func() {
err := io.EOF
wrapped := errors.Wrap(err, "some other message")
wrappedTwo := errors.Wrap(wrapped, "last error")
Expect(errors.Cause(wrappedTwo)).To(Equal(err))
Expect(errors.DisplayMessage(err)).To(Equal("Internal Error"))
})
It("can get error cause of a pkg/errors", func() {
msg := "an error"
err := pkgErrors.New(msg)
wrapped := pkgErrors.Wrap(err, "some other message")
Expect(errors.Cause(wrapped)).To(Equal(err))
Expect(errors.DisplayMessage(err)).To(Equal("Internal Error"))
})
It("allows to change display message", func() {
msg := "an error with a message"
err := errors.WithDisplayMessage(errors.New(msg), "We had a problem")
Expect(errors.Code(err)).To(Equal(errors.InternalError))
Expect(errors.DisplayMessage(err)).To(Equal("We had a problem"))
})
It("allows to change display message of custom error", func() {
msg := "an error with a message"
err := errors.WithDisplayMessage(errors.NotFound.New(msg), "The record was not found")
Expect(errors.Code(err)).To(Equal(errors.NotFound))
Expect(errors.DisplayMessage(err)).To(Equal("The record was not found"))
})
It("can get stack trace of non go-error error", func() {
err := io.EOF
Expect(fmt.Sprintf("%+v", errors.StackTrace(err))).To(ContainSubstring("/errors_test.go:"))
})
Describe("#Code", func() {
It("should return a wrapped error code", func() {
err := fmt.Errorf("testing: %w", errors.InvalidArgument.New("invalid argument"))
Expect(errors.Code(err)).To(Equal(errors.InvalidArgument))
})
})
})