@@ -9,12 +9,12 @@ import (
99// TestTypedRedisErrors tests that typed Redis errors are created correctly
1010func TestTypedRedisErrors (t * testing.T ) {
1111 tests := []struct {
12- name string
13- errorMsg string
14- expectedType interface {}
15- expectedMsg string
16- checkFunc func (error ) bool
17- extractAddr func (error ) string
12+ name string
13+ errorMsg string
14+ expectedType interface {}
15+ expectedMsg string
16+ checkFunc func (error ) bool
17+ extractAddr func (error ) string
1818 }{
1919 {
2020 name : "LOADING error" ,
@@ -30,6 +30,13 @@ func TestTypedRedisErrors(t *testing.T) {
3030 expectedMsg : "READONLY You can't write against a read only replica" ,
3131 checkFunc : IsReadOnlyError ,
3232 },
33+ {
34+ name : "READONLY error from Lua script" ,
35+ errorMsg : "ERR Error running script (call to f_xxx): @user_script:1: -READONLY You can't write against a read only replica" ,
36+ expectedType : RedisError ("" ), // Lua script errors are parsed as generic RedisError (string type)
37+ expectedMsg : "ERR Error running script (call to f_xxx): @user_script:1: -READONLY You can't write against a read only replica" ,
38+ checkFunc : IsReadOnlyError , // But IsReadOnlyError should still detect it
39+ },
3340 {
3441 name : "MOVED error" ,
3542 errorMsg : "MOVED 3999 127.0.0.1:6381" ,
@@ -144,8 +151,17 @@ func TestTypedRedisErrors(t *testing.T) {
144151 }
145152
146153 // Check error type using errors.As
147- if ! errors .As (err , & tt .expectedType ) {
148- t .Errorf ("Error type mismatch: expected %T, got %T" , tt .expectedType , err )
154+ // Special handling for RedisError (string type) - use type assertion instead
155+ if _ , ok := tt .expectedType .(RedisError ); ok {
156+ // For RedisError, check using type assertion
157+ if _ , ok := err .(RedisError ); ! ok {
158+ t .Errorf ("Error type mismatch: expected RedisError, got %T" , err )
159+ }
160+ } else {
161+ // For other types, use errors.As
162+ if ! errors .As (err , & tt .expectedType ) {
163+ t .Errorf ("Error type mismatch: expected %T, got %T" , tt .expectedType , err )
164+ }
149165 }
150166
151167 // Check using the helper function
@@ -181,6 +197,11 @@ func TestWrappedTypedErrors(t *testing.T) {
181197 errorMsg : "READONLY You can't write against a read only replica" ,
182198 checkFunc : IsReadOnlyError ,
183199 },
200+ {
201+ name : "Wrapped READONLY error from Lua script" ,
202+ errorMsg : "ERR Error running script (call to f_xxx): @user_script:1: -READONLY You can't write against a read only replica" ,
203+ checkFunc : IsReadOnlyError ,
204+ },
184205 {
185206 name : "Wrapped CLUSTERDOWN error" ,
186207 errorMsg : "CLUSTERDOWN The cluster is down" ,
@@ -389,4 +410,3 @@ func TestBackwardCompatibility(t *testing.T) {
389410 })
390411 }
391412}
392-
0 commit comments