@@ -33,6 +33,19 @@ PHPAPI void redis_stream_close(RedisSock *redis_sock TSRMLS_DC) {
33
33
}
34
34
}
35
35
36
+ PHPAPI void redis_throw_exception (zend_class_entry * exception_ce , const char * file , int line , const char * cmd , const char * cause , RedisSock * redis_sock TSRMLS_DC )
37
+ {
38
+ char message [1024 ];
39
+ if (!cmd ) {
40
+ sprintf (message , "%s in %s, line %d (%s:%d)" , cause , file , line , redis_sock -> host , redis_sock -> port );
41
+ }
42
+ else {
43
+ sprintf (message , "%s in %s, line %d, command: %s (%s:%d)" , cause , file , line , cmd , redis_sock -> host , redis_sock -> port );
44
+ }
45
+ php_log_err (message );
46
+ zend_throw_exception (redis_exception_ce , message , 0 TSRMLS_CC );
47
+ }
48
+
36
49
PHPAPI int redis_check_eof (RedisSock * redis_sock TSRMLS_DC )
37
50
{
38
51
int eof ;
@@ -41,32 +54,31 @@ PHPAPI int redis_check_eof(RedisSock *redis_sock TSRMLS_DC)
41
54
if (!redis_sock -> stream ) {
42
55
return -1 ;
43
56
}
44
-
45
57
eof = php_stream_eof (redis_sock -> stream );
46
58
for (; eof ; count ++ ) {
47
59
if ((MULTI == redis_sock -> mode ) || redis_sock -> watching || count == 10 ) { /* too many failures */
48
- if (redis_sock -> stream ) { /* close stream if still here */
60
+ if (redis_sock -> stream ) { /* close stream if still here */
49
61
redis_stream_close (redis_sock TSRMLS_CC );
50
62
redis_sock -> stream = NULL ;
51
63
redis_sock -> mode = ATOMIC ;
52
64
redis_sock -> status = REDIS_SOCK_STATUS_FAILED ;
53
65
redis_sock -> watching = 0 ;
66
+ }
67
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "Connection lost" , redis_sock TSRMLS_CC );
68
+ return -1 ;
54
69
}
55
- zend_throw_exception (redis_exception_ce , "Connection lost" , 0 TSRMLS_CC );
56
- return -1 ;
57
- }
58
- if (redis_sock -> stream ) { /* close existing stream before reconnecting */
70
+ if (redis_sock -> stream ) { /* close existing stream before reconnecting */
59
71
redis_stream_close (redis_sock TSRMLS_CC );
60
72
redis_sock -> stream = NULL ;
61
73
redis_sock -> mode = ATOMIC ;
62
74
redis_sock -> watching = 0 ;
63
- }
64
- // Wait for a while before trying to reconnect
65
- if (redis_sock -> retry_interval ) {
66
- // Random factor to avoid having several (or many) concurrent connections trying to reconnect at the same time
67
- long retry_interval = (count ? redis_sock -> retry_interval : (random () % redis_sock -> retry_interval ));
68
- usleep (retry_interval );
69
- }
75
+ }
76
+ // Wait for a while before trying to reconnect
77
+ if (redis_sock -> retry_interval ) {
78
+ // Random factor to avoid having several (or many) concurrent connections trying to reconnect at the same time
79
+ long retry_interval = (count ? redis_sock -> retry_interval : (random () % redis_sock -> retry_interval ));
80
+ usleep (retry_interval );
81
+ }
70
82
redis_sock_connect (redis_sock TSRMLS_CC ); /* reconnect */
71
83
if (redis_sock -> stream ) { /* check for EOF again. */
72
84
eof = php_stream_eof (redis_sock -> stream );
@@ -115,7 +127,7 @@ PHPAPI zval *redis_sock_read_multibulk_reply_zval(INTERNAL_FUNCTION_PARAMETERS,
115
127
redis_sock -> status = REDIS_SOCK_STATUS_FAILED ;
116
128
redis_sock -> mode = ATOMIC ;
117
129
redis_sock -> watching = 0 ;
118
- zend_throw_exception (redis_exception_ce , "read error on connection" , 0 TSRMLS_CC );
130
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "read error on connection" , redis_sock TSRMLS_CC );
119
131
return NULL ;
120
132
}
121
133
@@ -158,7 +170,7 @@ PHPAPI char *redis_sock_read_bulk_reply(RedisSock *redis_sock, int bytes TSRMLS_
158
170
got = php_stream_read (redis_sock -> stream , reply + offset , bytes - offset );
159
171
if (got <= 0 ) {
160
172
/* Error or EOF */
161
- zend_throw_exception (redis_exception_ce , "socket error on read socket" , 0 TSRMLS_CC );
173
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "socket error on read socket" , redis_sock TSRMLS_CC );
162
174
break ;
163
175
}
164
176
offset += got ;
@@ -191,7 +203,7 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
191
203
redis_sock -> status = REDIS_SOCK_STATUS_FAILED ;
192
204
redis_sock -> mode = ATOMIC ;
193
205
redis_sock -> watching = 0 ;
194
- zend_throw_exception (redis_exception_ce , "read error on connection" , 0 TSRMLS_CC );
206
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "read error on connection" , redis_sock TSRMLS_CC );
195
207
return NULL ;
196
208
}
197
209
@@ -201,7 +213,7 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
201
213
redis_sock_set_err (redis_sock , inbuf + 1 , err_len );
202
214
/* stale data */
203
215
if (memcmp (inbuf + 1 , "-ERR SYNC " , 10 ) == 0 ) {
204
- zend_throw_exception (redis_exception_ce , "SYNC with master in progress" , 0 TSRMLS_CC );
216
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "SYNC with master in progress" , redis_sock TSRMLS_CC );
205
217
}
206
218
return NULL ;
207
219
@@ -895,7 +907,7 @@ PHPAPI int redis_sock_read_multibulk_reply_zipped_with_flag(INTERNAL_FUNCTION_PA
895
907
redis_sock -> status = REDIS_SOCK_STATUS_FAILED ;
896
908
redis_sock -> mode = ATOMIC ;
897
909
redis_sock -> watching = 0 ;
898
- zend_throw_exception (redis_exception_ce , "read error on connection" , 0 TSRMLS_CC );
910
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "read error on connection" , redis_sock TSRMLS_CC );
899
911
return -1 ;
900
912
}
901
913
@@ -1271,7 +1283,7 @@ PHPAPI int redis_sock_read_multibulk_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSo
1271
1283
redis_sock -> status = REDIS_SOCK_STATUS_FAILED ;
1272
1284
redis_sock -> mode = ATOMIC ;
1273
1285
redis_sock -> watching = 0 ;
1274
- zend_throw_exception (redis_exception_ce , "read error on connection" , 0 TSRMLS_CC );
1286
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "read error on connection" , redis_sock TSRMLS_CC );
1275
1287
return -1 ;
1276
1288
}
1277
1289
@@ -1318,7 +1330,7 @@ PHPAPI int redis_sock_read_multibulk_reply_raw(INTERNAL_FUNCTION_PARAMETERS, Red
1318
1330
redis_sock -> status = REDIS_SOCK_STATUS_FAILED ;
1319
1331
redis_sock -> mode = ATOMIC ;
1320
1332
redis_sock -> watching = 0 ;
1321
- zend_throw_exception (redis_exception_ce , "read error on connection" , 0 TSRMLS_CC );
1333
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "read error on connection" , redis_sock TSRMLS_CC );
1322
1334
return -1 ;
1323
1335
}
1324
1336
@@ -1397,7 +1409,7 @@ PHPAPI int redis_sock_read_multibulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS, R
1397
1409
redis_sock -> status = REDIS_SOCK_STATUS_FAILED ;
1398
1410
redis_sock -> mode = ATOMIC ;
1399
1411
redis_sock -> watching = 0 ;
1400
- zend_throw_exception (redis_exception_ce , "read error on connection" , 0 TSRMLS_CC );
1412
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "read error on connection" , redis_sock TSRMLS_CC );
1401
1413
return -1 ;
1402
1414
}
1403
1415
@@ -1449,7 +1461,7 @@ PHPAPI int redis_sock_read_multibulk_reply_assoc(INTERNAL_FUNCTION_PARAMETERS, R
1449
1461
PHPAPI int redis_sock_write (RedisSock * redis_sock , char * cmd , size_t sz TSRMLS_DC )
1450
1462
{
1451
1463
if (redis_sock && redis_sock -> status == REDIS_SOCK_STATUS_DISCONNECTED ) {
1452
- zend_throw_exception (redis_exception_ce , "Connection closed" , 0 TSRMLS_CC );
1464
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , cmd , "Connection closed" , redis_sock TSRMLS_CC );
1453
1465
return -1 ;
1454
1466
}
1455
1467
if (-1 == redis_check_eof (redis_sock TSRMLS_CC )) {
@@ -1645,7 +1657,7 @@ redis_sock_gets(RedisSock *redis_sock, char *buf, int buf_size, size_t *line_siz
1645
1657
redis_sock -> watching = 0 ;
1646
1658
1647
1659
// Throw a read error exception
1648
- zend_throw_exception (redis_exception_ce , "read error on connection" , 0 TSRMLS_CC );
1660
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "read error on connection" , redis_sock TSRMLS_CC );
1649
1661
}
1650
1662
1651
1663
// We don't need \r\n
@@ -1667,7 +1679,7 @@ redis_read_reply_type(RedisSock *redis_sock, REDIS_REPLY_TYPE *reply_type, int *
1667
1679
1668
1680
// Attempt to read the reply-type byte
1669
1681
if ((* reply_type = php_stream_getc (redis_sock -> stream )) == EOF ) {
1670
- zend_throw_exception (redis_exception_ce , "socket error on read socket" , 0 TSRMLS_CC );
1682
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "socket error on read socket" , redis_sock TSRMLS_CC );
1671
1683
}
1672
1684
1673
1685
// If this is a BULK, MULTI BULK, or simply an INTEGER response, we can extract the value or size info here
@@ -1705,7 +1717,7 @@ redis_read_variant_line(RedisSock *redis_sock, REDIS_REPLY_TYPE reply_type, zval
1705
1717
// If this is an error response, check if it is a SYNC error, and throw in that case
1706
1718
if (reply_type == TYPE_ERR ) {
1707
1719
if (memcmp (inbuf , "ERR SYNC" , 9 ) == 0 ) {
1708
- zend_throw_exception (redis_exception_ce , "SYNC with master in progress" , 0 TSRMLS_CC );
1720
+ redis_throw_exception (redis_exception_ce , __FILE__ , __LINE__ , NULL , "SYNC with master in progress" , redis_sock TSRMLS_CC );
1709
1721
}
1710
1722
1711
1723
// Set our last error
0 commit comments