Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extend getLastError to all calls

Fixes GitHub issue #245.
  • Loading branch information...
commit 6153477c4f71cbfadaae628e47ea022136f0131f 1 parent 9eb217c
@nicolasff nicolasff authored
Showing with 14 additions and 3 deletions.
  1. +1 −1  README.markdown
  2. +3 −0  library.c
  3. +10 −2 tests/TestRedis.php
View
2  README.markdown
@@ -2472,7 +2472,7 @@ $redis->script('exists', $script1, [$script2, $script3, ...]);
## getLastError
##### Description
-The last error message (if any) returned from a SCRIPT call
+The last error message (if any)
##### Parameters
*none*
##### Return Value
View
3  library.c
@@ -172,6 +172,7 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
{
char inbuf[1024];
char *resp = NULL;
+ size_t err_len;
if(-1 == redis_check_eof(redis_sock TSRMLS_CC)) {
return NULL;
@@ -189,6 +190,8 @@ PHPAPI char *redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC)
switch(inbuf[0]) {
case '-':
+ err_len = strlen(inbuf+1) - 2;
+ redis_sock_set_err(redis_sock, inbuf+1, err_len);
/* stale data */
if(memcmp(inbuf + 1, "-ERR SYNC ", 10) == 0) {
zend_throw_exception(redis_exception_ce, "SYNC with master in progress", 0 TSRMLS_CC);
View
12 tests/TestRedis.php
@@ -3023,8 +3023,16 @@ public function testGetLastError() {
$this->redis->eval("not-a-lua-script");
// Now we should have an error
- $this->assertTrue(strlen($this->redis->getLastError()) > 0);
- }
+ $evalError = $this->redis->getLastError();
+ $this->assertTrue(strlen($evalError) > 0);
+
+ // test getLastError with a regular command
+ $this->redis->set('x', 'a');
+ $this->assertFalse($this->redis->incr('x'));
+ $incrError = $this->redis->getLastError();
+ $this->assertTrue($incrError !== $evalError); // error has changed
+ $this->assertTrue(strlen($incrError) > 0);
+ }
// Helper function to compare nested results -- from the php.net array_diff page, I believe
private function array_diff_recursive($aArray1, $aArray2) {

3 comments on commit 6153477

@michael-grunder

That is so fantastic. Really useful for debugging commands :)

@nicolasff
Owner

Not exactly a big change :)
I was wondering, do you think commands that do not return an error should clear the message? Because at the moment, if you run into a problem and then call another method again, the error flag is still set... Well, it is called getLastError, not getErrorForLastCall.
I'm not sure what would be best. I don't think getLastError should clear it though, a "getter" function should not have side-effects imo. Maybe a clearLastError would be a good compromise?

@michael-grunder

I think clearLastError is a good compromise. Honestly, I think how people will use this is test for something like $result === false and then test getLastError().

The other way to go would be for functions to setLastError() to NULL when they don't fail, but I don't see this as necessary. People will use it to figure out why that one call to ZUNIONSTORE isn't working or whatever. :)

Please sign in to comment.
Something went wrong with that request. Please try again.