Skip to content

Commit

Permalink
[1.x] Improve redis exception (#168)
Browse files Browse the repository at this point in the history
* improve redis exception

* Fix code styling

---------

Co-authored-by: timacdonald <[email protected]>
  • Loading branch information
timacdonald and timacdonald authored Dec 7, 2023
1 parent 2c97b11 commit 3936f1a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/Support/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ public function pipeline(callable $closure): array
protected function handle(array $args): mixed
{
try {
return tap($this->run($args), function ($result) {
return tap($this->run($args), function ($result) use ($args) {
if ($result === false && $this->client() instanceof PhpRedis) {
throw new RedisClientException($this->client()->getLastError() ?? 'An unknown error occurred.');
throw RedisServerException::whileRunningCommand(implode(' ', $args), $this->client()->getLastError() ?? 'An unknown error occurred.');
}
});
} catch (PredisServerException $e) {
throw new RedisClientException($e->getMessage(), previous: $e);
throw RedisServerException::whileRunningCommand(implode(' ', $args), $e->getMessage(), previous: $e);
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/Support/RedisClientException.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/Support/RedisServerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Laravel\Pulse\Support;

use RuntimeException;
use Throwable;

/**
* @internal
*/
class RedisServerException extends RuntimeException
{
/**
* Create an exception from the client's error message.
*/
public static function whileRunningCommand(string $command, string $message, ?Throwable $previous = null): self
{
if (str_starts_with($message, 'ERR syntax error')) {
$message = "The Redis version does not support the command or some of its arguments [{$command}]. Redis error: [{$message}].";
} else {
$message = "Error running command [{$command}]. Redis error: [{$message}].";
}

return new self($message, previous: $previous);
}
}
8 changes: 6 additions & 2 deletions tests/Feature/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Laravel\Pulse\Entry;
use Laravel\Pulse\Ingests\RedisIngest;
use Laravel\Pulse\Support\RedisAdapter;
use Laravel\Pulse\Support\RedisClientException;
use Laravel\Pulse\Support\RedisServerException;
use Tests\StorageFake;

beforeEach(function () {
Expand Down Expand Up @@ -137,4 +137,8 @@
$redis = new RedisAdapter(Redis::connection(), App::make('config'));

$redis->xtrim('stream-name', 'FOO', 'a', 'xyz');
})->with(['predis', 'phpredis'])->throws(RedisClientException::class, 'ERR syntax error');
})->with(['predis', 'phpredis'])->throws(RedisServerException::class, 'The Redis version does not support the command or some of its arguments [XTRIM laravel_database_stream-name FOO a xyz]. Redis error: [ERR syntax error].');

it('prepends the error message with the run command', function () {
throw RedisServerException::whileRunningCommand('FOO BAR', 'Something happened');
})->throws(RedisServerException::class, 'Error running command [FOO BAR]. Redis error: [Something happened].');

0 comments on commit 3936f1a

Please sign in to comment.