This repository was archived by the owner on Aug 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
83 lines (66 loc) · 2.35 KB
/
Copy patherrors_test.go
File metadata and controls
83 lines (66 loc) · 2.35 KB
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
package errors
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestWithErrorWithMessage(t *testing.T) {
a := require.New(t)
rootErr := New("root error")
withError := WithError(rootErr, NotValid)
a.Equal("root error, with error \"not valid\"", withError.Error())
a.True(Is(withError, NotValid))
withMessage := WithMessage(withError, "error message")
a.Equal("root error, with error \"not valid\"", withError.Error())
a.True(Is(withError, NotValid))
var m Message
a.True(As(withMessage, &m))
a.Equal("error message", m.Value)
wrap1 := Wrap(withMessage, "wrap1")
a.Equal("wrap1: root error, with error \"not valid\", with message \"error message\"", wrap1.Error())
}
type customError struct {
Val string
}
func (ce customError) Error() string {
return ce.Val
}
func TestWithErrorWithMessage_CustomRootError(t *testing.T) {
a := require.New(t)
rootErr := customError{Val: "custom error"}
withError := WithError(rootErr, NotValid)
a.Equal("custom error, with error \"not valid\"", withError.Error())
a.True(Is(withError, NotValid))
var ce customError
a.True(As(withError, &ce))
withMessage := WithMessage(withError, "error message")
a.Equal("custom error, with error \"not valid\"", withError.Error())
a.True(Is(withError, NotValid))
var m Message
a.True(As(withMessage, &m))
a.Equal("error message", m.Value)
wrap1 := Wrap(withMessage, "wrap1")
a.Equal("wrap1: custom error, with error \"not valid\", with message \"error message\"", wrap1.Error())
}
func TestWithErrorMessage(t *testing.T) {
a := require.New(t)
rootErr := New("root error")
withErrorMessage := WithErrorMessage(rootErr, NotValid, "message")
a.Equal("root error, with error \"not valid\", with message \"message\"",
withErrorMessage.Error())
}
func TestWithErrorMessage_CustomRootError(t *testing.T) {
a := require.New(t)
rootErr := customError{Val: "custom error"}
withErrorMessage := WithErrorMessage(rootErr, NotValid, "message")
a.Equal("custom error, with error \"not valid\", with message \"message\"",
withErrorMessage.Error())
}
func TestWrapWithErrorMessage(t *testing.T) {
a := require.New(t)
rootErr := New("root error")
errorMsg := "message"
wrapMsg := "wrap"
withErrorMessage := WrapWithErrorMessage(rootErr, NotValid, errorMsg, wrapMsg)
a.Equal("wrap: root error, with error \"not valid\", with message \"message\"",
withErrorMessage.Error())
}