Skip to content

Commit 6153477

Browse files
committed
Extend getLastError to all calls
Fixes GitHub issue phpredis#245.
1 parent 9eb217c commit 6153477

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,7 @@ $redis->script('exists', $script1, [$script2, $script3, ...]);
24722472

24732473
## getLastError
24742474
##### Description
2475-
The last error message (if any) returned from a SCRIPT call
2475+
The last error message (if any)
24762476
##### Parameters
24772477
*none*
24782478
##### Return Value

library.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
172172
{
173173
char inbuf[1024];
174174
char *resp = NULL;
175+
size_t err_len;
175176

176177
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
177178
return NULL;
@@ -189,6 +190,8 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
189190

190191
switch(inbuf[0]) {
191192
case '-':
193+
err_len = strlen(inbuf+1) - 2;
194+
redis_sock_set_err(redis_sock, inbuf+1, err_len);
192195
/* stale data */
193196
if(memcmp(inbuf + 1, "-ERR SYNC ", 10) == 0) {
194197
zend_throw_exception(redis_exception_ce, "SYNC with master in progress", 0 TSRMLS_CC);

tests/TestRedis.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,8 +3023,16 @@ public function testGetLastError() {
30233023
$this->redis->eval("not-a-lua-script");
30243024

30253025
// Now we should have an error
3026-
$this->assertTrue(strlen($this->redis->getLastError()) > 0);
3027-
}
3026+
$evalError = $this->redis->getLastError();
3027+
$this->assertTrue(strlen($evalError) > 0);
3028+
3029+
// test getLastError with a regular command
3030+
$this->redis->set('x', 'a');
3031+
$this->assertFalse($this->redis->incr('x'));
3032+
$incrError = $this->redis->getLastError();
3033+
$this->assertTrue($incrError !== $evalError); // error has changed
3034+
$this->assertTrue(strlen($incrError) > 0);
3035+
}
30283036

30293037
// Helper function to compare nested results -- from the php.net array_diff page, I believe
30303038
private function array_diff_recursive($aArray1, $aArray2) {

0 commit comments

Comments
 (0)