mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
fix(client): carry the pre-send failures in a type callers can trust
Review found the first attempt unsound. Connection validation and the dial before it run ahead of every send, so a failure raised there leaves the command unsent and nothing applied - but a numeric code cannot say so. A post-send error response is parsed into the same Exception holding the SERVER's code, HostUnreachable included, so any caller keying on the code would replay an operation that was already transmitted and could apply it twice. The typed class constant also broke the PHP 8.0-8.2 this package still supports. The signal is now a type: UnsentException, raised only by the client and only before it has sent anything, answering isUnsentError() true where the base answers false. It extends Exception, so every existing catch behaves identically, and the messages are unchanged so the client's own internal reconnect check still matches. The failed reconnect dial throws it too - review caught that it escapes validateConnection while the command is just as unsent. Covered: an unsent failure is distinguishable by type and still caught as the package exception, a post-send timeout is never unsent, and a server response reporting HostUnreachable is never unsent. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b395b0e35b
commit
232d355fe8
4 files changed
+60
-29
No files matched your search
+4
-5
@@ -303,7 +303,7 @@ class Client
|
||||
try {
|
||||
if (!$this->client->connect($this->host, $this->port, $budget)) {
|
||||
$this->invalidate();
|
||||
throw new Exception("Failed to connect to MongoDB at {$this->host}:{$this->port}");
|
||||
throw new UnsentException("Failed to connect to MongoDB at {$this->host}:{$this->port}");
|
||||
}
|
||||
|
||||
$this->isConnected = true;
|
||||
@@ -2179,15 +2179,14 @@ class Client
|
||||
}
|
||||
|
||||
// Both throws are raised ahead of the send, so the command never
|
||||
// reached the server and a caller may safely replay it. They carry
|
||||
// HostUnreachable to say so in a code rather than in text.
|
||||
// reached the server and a caller may safely replay it.
|
||||
if (!$this->isConnected) {
|
||||
throw new Exception('Client is not connected to MongoDB', Exception::HOST_UNREACHABLE);
|
||||
throw new UnsentException('Client is not connected to MongoDB');
|
||||
}
|
||||
|
||||
if (!$this->client->isConnected()) {
|
||||
$this->isConnected = false;
|
||||
throw new Exception('Connection to MongoDB has been lost', Exception::HOST_UNREACHABLE);
|
||||
throw new UnsentException('Connection to MongoDB has been lost');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-14
@@ -9,19 +9,6 @@ namespace Utopia\Mongo;
|
||||
*/
|
||||
class Exception extends \Exception
|
||||
{
|
||||
/**
|
||||
* The connection was gone before the command left the client.
|
||||
*
|
||||
* MongoDB's own HostUnreachable. Carried by every throw raised out of
|
||||
* connection validation, which runs ahead of the send: the command was
|
||||
* never on the wire, so nothing was applied and a caller may safely
|
||||
* replay it. Distinguishing this from a post-send failure used to be
|
||||
* possible only by matching the message text, which callers cannot do
|
||||
* safely - a message quotes caller-chosen values, a code cannot be
|
||||
* spelled by a caller.
|
||||
*/
|
||||
public const int HOST_UNREACHABLE = 6;
|
||||
|
||||
protected array $errorLabels = [];
|
||||
protected ?array $writeErrors = null;
|
||||
protected ?array $writeConcernErrors = null;
|
||||
@@ -121,10 +108,15 @@ class Exception extends \Exception
|
||||
/**
|
||||
* Whether the command was still unsent when this failure was raised, so
|
||||
* nothing was applied and the caller may replay it.
|
||||
*
|
||||
* False here on purpose: this type is what a server's own error response
|
||||
* is parsed into, and that response carries the server's code. Only
|
||||
* {@see UnsentException} — raised solely by the client, solely before it
|
||||
* has sent anything — answers true.
|
||||
*/
|
||||
public function isUnsentError(): bool
|
||||
{
|
||||
return $this->code === self::HOST_UNREACHABLE;
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isTimeoutError(): bool
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Mongo;
|
||||
|
||||
/**
|
||||
* A failure raised before the command reached the server.
|
||||
*
|
||||
* Connection validation and the dial that precedes it run ahead of every
|
||||
* send, so a failure raised there leaves the command unsent and nothing
|
||||
* applied — the one class of failure a caller may safely replay.
|
||||
*
|
||||
* The distinction is carried by the TYPE and nothing else. A message quotes
|
||||
* caller-chosen values, so callers cannot classify on text; and an error code
|
||||
* cannot prove it either, because a post-send error response carries the
|
||||
* server's own code into an ordinary {@see Exception} — including codes like
|
||||
* HostUnreachable that look pre-send. Only the client raises this type, and
|
||||
* only before it has sent anything.
|
||||
*/
|
||||
class UnsentException extends Exception
|
||||
{
|
||||
public function isUnsentError(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+25
-10
@@ -4,22 +4,27 @@ namespace Utopia\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Mongo\Exception;
|
||||
use Utopia\Mongo\UnsentException;
|
||||
|
||||
/**
|
||||
* Connection validation runs ahead of the send, so every failure it raises
|
||||
* leaves the command unsent and nothing applied - the one class of failure a
|
||||
* caller may safely replay. That distinction was previously legible only in
|
||||
* the message text, which a caller cannot classify on safely, and a cloud
|
||||
* pool consequently surfaced a replayable refusal to the client as a 500.
|
||||
* Connection validation and the dial before it run ahead of every send, so a
|
||||
* failure raised there leaves the command unsent and nothing applied - the one
|
||||
* class of failure a caller may safely replay. That distinction was previously
|
||||
* legible only in the message text, and a cloud pool consequently surfaced a
|
||||
* replayable refusal to clients as a 500 during a backing resize.
|
||||
*
|
||||
* It is carried by the type and nothing else. A code cannot carry it: a
|
||||
* post-send error response is parsed into an ordinary Exception holding the
|
||||
* SERVER's code, which includes codes that look pre-send.
|
||||
*/
|
||||
final class UnsentErrorTest extends TestCase
|
||||
{
|
||||
public function testAnUnsentFailureIsDistinguishableByCode(): void
|
||||
public function testAnUnsentFailureIsDistinguishableByType(): void
|
||||
{
|
||||
$unsent = new Exception('Client is not connected to MongoDB', Exception::HOST_UNREACHABLE);
|
||||
$unsent = new UnsentException('Client is not connected to MongoDB');
|
||||
|
||||
$this->assertTrue($unsent->isUnsentError());
|
||||
$this->assertFalse($unsent->isTimeoutError(), 'An unsent failure must not be confused with a post-send timeout');
|
||||
$this->assertInstanceOf(Exception::class, $unsent, 'Callers that catch the package exception must still catch it');
|
||||
}
|
||||
|
||||
public function testAPostSendTimeoutIsNotReportedAsUnsent(): void
|
||||
@@ -33,8 +38,18 @@ final class UnsentErrorTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testAnUncodedFailureIsNotAssumedUnsent(): void
|
||||
/**
|
||||
* The reason a code cannot carry this: the server picks it. A response
|
||||
* that happens to report HostUnreachable is still a post-send answer, and
|
||||
* replaying the operation it answered could apply it twice.
|
||||
*/
|
||||
public function testAServerErrorResponseIsNeverReportedAsUnsent(): void
|
||||
{
|
||||
$this->assertFalse(new Exception('something went wrong')->isUnsentError());
|
||||
$response = new \stdClass();
|
||||
$response->code = 6;
|
||||
$response->codeName = 'HostUnreachable';
|
||||
$response->errmsg = 'host unreachable';
|
||||
|
||||
$this->assertFalse(Exception::fromResponse($response)->isUnsentError());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user