Skip to content

Commit

Permalink
Added RedisException::getStreamMetadata() method to assist in debuggi…
Browse files Browse the repository at this point in the history
…ng of read errors
  • Loading branch information
freost committed Jan 17, 2025
1 parent bfc6aea commit fadf369
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### 11.0.2 <small>(2025-01-17)</small>

#### New

* Added `RedisException::getStreamMetadata()` method to assist in debugging of read errors.

--------------------------------------------------------

### 10.0.12, 11.0.1 <small>(2025-01-16)</small>

#### Compatibility
Expand Down
4 changes: 2 additions & 2 deletions src/mako/Mako.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Mako
/**
* Mako version.
*/
public const string VERSION = '11.0.0';
public const string VERSION = '11.0.2';

/**
* Mako major version.
Expand All @@ -30,5 +30,5 @@ final class Mako
/**
* Mako patch version.
*/
public const int VERSION_PATCH = 0;
public const int VERSION_PATCH = 2;
}
17 changes: 7 additions & 10 deletions src/mako/redis/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,18 @@ protected function createConnection(string $host, int $port)
}

/**
* Appends the read error reason to the error message if possible.
* Returns the message with the read error reason to the
* error message if possible along with the stream metadata.
*/
protected function appendReadErrorReason($message): string
protected function getMessageAndStreamMetadata($message): array
{
$metadata = stream_get_meta_data($this->connection);

if ($metadata['timed_out']) {
return "{$message} The stream timed out while waiting for data.";
$message = "{$message} The stream timed out while waiting for data.";
}

if ($metadata['blocked']) {
return "{$message} The stream is in blocking I/O mode.";
}

return $message;
return ['message' => $message, 'streamMetadata' => $metadata];
}

/**
Expand All @@ -163,7 +160,7 @@ public function readLine(): string
$line = fgets($this->connection);

if ($line === false || $line === '') {
throw new RedisException($this->appendReadErrorReason('Failed to read line from the server.'));
throw new RedisException(...$this->getMessageAndStreamMetadata('Failed to read line from the server.'));
}

return $line;
Expand All @@ -182,7 +179,7 @@ public function read(int $bytes): string
$chunk = fread($this->connection, min($bytesLeft, 4096));

if ($chunk === false) {
throw new RedisException($this->appendReadErrorReason('Failed to read data from the server.'));
throw new RedisException(...$this->getMessageAndStreamMetadata('Failed to read data from the server.'));
}

$data .= $chunk;
Expand Down
19 changes: 19 additions & 0 deletions src/mako/redis/exceptions/RedisException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,30 @@
namespace mako\redis\exceptions;

use RuntimeException;
use Throwable;

/**
* Redis exception.
*/
class RedisException extends RuntimeException
{
/**
* Constructor.
*/
public function __construct(
string $message = '',
int $code = 0,
?Throwable $previous = null,
protected ?array $streamMetadata = null
) {
parent::__construct($message, $code, $previous);
}

/**
* Returns the stream metadata.
*/
public function getStreamMetadata(): ?array
{
return $this->streamMetadata;
}
}

0 comments on commit fadf369

Please sign in to comment.