Skip to content

Commit 3c806a3

Browse files
committed
refactor: match the result code with IsErrorAnyOf even if an error is wrapped
1 parent 21d1415 commit 3c806a3

File tree

4 files changed

+164
-4
lines changed

4 files changed

+164
-4
lines changed

error.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ldap
22

33
import (
4+
"errors"
45
"fmt"
56

67
ber "github.com/go-asn1-ber/asn1-ber"
@@ -241,8 +242,8 @@ func IsErrorAnyOf(err error, codes ...uint16) bool {
241242
return false
242243
}
243244

244-
serverError, ok := err.(*Error)
245-
if !ok {
245+
var serverError *Error
246+
if !errors.As(err, &serverError) {
246247
return false
247248
}
248249

error_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ldap
22

33
import (
44
"errors"
5+
"fmt"
56
"io"
67
"net"
78
"strings"
@@ -11,6 +12,84 @@ import (
1112
ber "github.com/go-asn1-ber/asn1-ber"
1213
)
1314

15+
// TestWrappedError tests that match the result code when an error is wrapped.
16+
func TestWrappedError(t *testing.T) {
17+
resultCodes := []uint16{
18+
LDAPResultProtocolError,
19+
LDAPResultBusy,
20+
ErrorNetwork,
21+
}
22+
23+
tests := []struct {
24+
name string
25+
err error
26+
codes []uint16
27+
expected bool
28+
}{
29+
// success
30+
{
31+
name: "a normal error",
32+
err: &Error{
33+
ResultCode: ErrorNetwork,
34+
},
35+
codes: resultCodes,
36+
expected: true,
37+
},
38+
39+
{
40+
name: "a wrapped error",
41+
err: fmt.Errorf("wrap: %w", &Error{
42+
ResultCode: LDAPResultBusy,
43+
}),
44+
codes: resultCodes,
45+
expected: true,
46+
},
47+
48+
{
49+
name: "multiple wrapped error",
50+
err: fmt.Errorf("second: %w",
51+
fmt.Errorf("first: %w",
52+
&Error{
53+
ResultCode: LDAPResultProtocolError,
54+
},
55+
),
56+
),
57+
codes: resultCodes,
58+
expected: true,
59+
},
60+
61+
// failure
62+
{
63+
name: "not match a normal error",
64+
err: &Error{
65+
ResultCode: LDAPResultSuccess,
66+
},
67+
codes: resultCodes,
68+
expected: false,
69+
},
70+
71+
{
72+
name: "not match a wrapped error",
73+
err: fmt.Errorf("wrap: %w", &Error{
74+
ResultCode: LDAPResultNoSuchObject,
75+
}),
76+
codes: resultCodes,
77+
expected: false,
78+
},
79+
}
80+
81+
for _, tt := range tests {
82+
tt := tt
83+
t.Run(tt.name, func(t *testing.T) {
84+
t.Parallel()
85+
actual := IsErrorAnyOf(tt.err, tt.codes...)
86+
if tt.expected != actual {
87+
t.Errorf("expected %t, but got %t", tt.expected, actual)
88+
}
89+
})
90+
}
91+
}
92+
1493
// TestNilPacket tests that nil packets don't cause a panic.
1594
func TestNilPacket(t *testing.T) {
1695
// Test for nil packet

v3/error.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ldap
22

33
import (
4+
"errors"
45
"fmt"
56

67
ber "github.com/go-asn1-ber/asn1-ber"
@@ -241,8 +242,8 @@ func IsErrorAnyOf(err error, codes ...uint16) bool {
241242
return false
242243
}
243244

244-
serverError, ok := err.(*Error)
245-
if !ok {
245+
var serverError *Error
246+
if !errors.As(err, &serverError) {
246247
return false
247248
}
248249

v3/error_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ldap
22

33
import (
44
"errors"
5+
"fmt"
56
"io"
67
"net"
78
"strings"
@@ -11,6 +12,84 @@ import (
1112
ber "github.com/go-asn1-ber/asn1-ber"
1213
)
1314

15+
// TestWrappedError tests that match the result code when an error is wrapped.
16+
func TestWrappedError(t *testing.T) {
17+
resultCodes := []uint16{
18+
LDAPResultProtocolError,
19+
LDAPResultBusy,
20+
ErrorNetwork,
21+
}
22+
23+
tests := []struct {
24+
name string
25+
err error
26+
codes []uint16
27+
expected bool
28+
}{
29+
// success
30+
{
31+
name: "a normal error",
32+
err: &Error{
33+
ResultCode: ErrorNetwork,
34+
},
35+
codes: resultCodes,
36+
expected: true,
37+
},
38+
39+
{
40+
name: "a wrapped error",
41+
err: fmt.Errorf("wrap: %w", &Error{
42+
ResultCode: LDAPResultBusy,
43+
}),
44+
codes: resultCodes,
45+
expected: true,
46+
},
47+
48+
{
49+
name: "multiple wrapped error",
50+
err: fmt.Errorf("second: %w",
51+
fmt.Errorf("first: %w",
52+
&Error{
53+
ResultCode: LDAPResultProtocolError,
54+
},
55+
),
56+
),
57+
codes: resultCodes,
58+
expected: true,
59+
},
60+
61+
// failure
62+
{
63+
name: "not match a normal error",
64+
err: &Error{
65+
ResultCode: LDAPResultSuccess,
66+
},
67+
codes: resultCodes,
68+
expected: false,
69+
},
70+
71+
{
72+
name: "not match a wrapped error",
73+
err: fmt.Errorf("wrap: %w", &Error{
74+
ResultCode: LDAPResultNoSuchObject,
75+
}),
76+
codes: resultCodes,
77+
expected: false,
78+
},
79+
}
80+
81+
for _, tt := range tests {
82+
tt := tt
83+
t.Run(tt.name, func(t *testing.T) {
84+
t.Parallel()
85+
actual := IsErrorAnyOf(tt.err, tt.codes...)
86+
if tt.expected != actual {
87+
t.Errorf("expected %t, but got %t", tt.expected, actual)
88+
}
89+
})
90+
}
91+
}
92+
1493
// TestNilPacket tests that nil packets don't cause a panic.
1594
func TestNilPacket(t *testing.T) {
1695
// Test for nil packet

0 commit comments

Comments
 (0)