-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
186 lines (155 loc) · 4.56 KB
/
error_test.go
File metadata and controls
186 lines (155 loc) · 4.56 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2026 The Gopherly Authors
// Copyright 2025 Company.info B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !integration
package synthra
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfigError_Error(t *testing.T) {
t.Parallel()
baseErr := errors.New("base error")
tests := []struct {
name string
err *ConfigError
wantMsg string
}{
{
name: "with path and err",
err: &ConfigError{
Op: OpLoad,
Path: "source[0]",
Err: baseErr,
},
wantMsg: "synthra: load source[0]: base error",
},
{
name: "with path but no err",
err: &ConfigError{
Op: OpLoad,
Path: "source[0]",
},
wantMsg: "synthra: load source[0]",
},
{
name: "without path with err",
err: &ConfigError{
Op: OpLoad,
Err: ErrNilContext,
},
wantMsg: "synthra: load: synthra: nil context",
},
{
name: "op only, no path, no err",
err: &ConfigError{Op: OpNew},
wantMsg: "synthra: new",
},
{
name: "nil receiver",
err: (*ConfigError)(nil),
wantMsg: "synthra: <nil>",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.wantMsg, tt.err.Error())
})
}
}
func TestConfigError_NilReceiver(t *testing.T) {
t.Parallel()
var ce *ConfigError
assert.Nil(t, ce.Unwrap())
}
func TestConfigError_Unwrap(t *testing.T) {
t.Parallel()
baseErr := errors.New("base error")
ce := NewConfigError(OpGet, "server.port", baseErr)
assert.Equal(t, baseErr, ce.Unwrap())
}
func TestNewConfigError(t *testing.T) {
t.Parallel()
baseErr := errors.New("base error")
ce := NewConfigError(OpNew, "WithFile", baseErr)
assert.Equal(t, OpNew, ce.Op)
assert.Equal(t, "WithFile", ce.Path)
assert.Equal(t, baseErr, ce.Err)
}
func TestConfigError_IsSentinelsViaUnwrap(t *testing.T) {
t.Parallel()
wrappedKey := fmt.Errorf("wrap: %w", ErrKeyNotFound)
ce := NewConfigError(OpGet, "k", wrappedKey)
assert.True(t, errors.Is(ce, ErrKeyNotFound))
ce2 := NewConfigError(OpLoad, "", ErrNilContext)
assert.True(t, errors.Is(ce2, ErrNilContext))
ce3 := NewConfigError(OpNew, "WithBinding", ErrNilConfig)
assert.True(t, errors.Is(ce3, ErrNilConfig))
}
func TestConfigError_ErrorWrapping(t *testing.T) {
t.Parallel()
originalErr := errors.New("original error")
t.Run("errors_Is_traverses_chain", func(t *testing.T) {
t.Parallel()
err := NewConfigError(OpGet, "port", fmt.Errorf("cast: %w", originalErr))
assert.True(t, errors.Is(err, originalErr))
var ce *ConfigError
require.True(t, errors.As(err, &ce))
assert.Equal(t, OpGet, ce.Op)
})
t.Run("errors_As_unwrap", func(t *testing.T) {
t.Parallel()
err := NewConfigError(OpLoad, "source[1]", originalErr)
var ce *ConfigError
require.True(t, errors.As(err, &ce))
assert.Equal(t, originalErr, ce.Unwrap())
})
}
func TestConfigError_Chaining(t *testing.T) {
t.Parallel()
originalErr := errors.New("original error")
firstErr := NewConfigError(OpLoad, "source[0]", originalErr)
secondErr := NewConfigError(OpLoad, "binding-decode", firstErr)
var inner *ConfigError
require.True(t, errors.As(secondErr, &inner))
assert.Equal(t, "binding-decode", inner.Path)
require.True(t, errors.Is(secondErr, originalErr))
}
func TestConfigError_JoinedErrors(t *testing.T) {
t.Parallel()
e1 := NewConfigError(OpNew, "WithFile", errors.New("a"))
e2 := NewConfigError(OpNew, "WithTag", errors.New("b"))
joined := errors.Join(e1, e2)
var first *ConfigError
require.True(t, errors.As(joined, &first))
assert.Equal(t, OpNew, first.Op)
unwrapMulti, ok := joined.(interface{ Unwrap() []error })
require.True(t, ok)
children := unwrapMulti.Unwrap()
require.Len(t, children, 2)
var ce0, ce1 *ConfigError
require.True(t, errors.As(children[0], &ce0))
require.True(t, errors.As(children[1], &ce1))
assert.Equal(t, "WithFile", ce0.Path)
assert.Equal(t, "WithTag", ce1.Path)
}
func TestSentinelErrors_KeyNotFoundWrapping(t *testing.T) {
t.Parallel()
err := fmt.Errorf("lookup: %w", ErrKeyNotFound)
assert.ErrorIs(t, err, ErrKeyNotFound)
}