diff --git a/src/Client.php b/src/Client.php index 2ad5494bd5..8c4c153a9a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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'); } } diff --git a/src/Exception.php b/src/Exception.php index 184d1bac22..61953c2bbf 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -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 diff --git a/src/UnsentException.php b/src/UnsentException.php new file mode 100644 index 0000000000..686a6e898c --- /dev/null +++ b/src/UnsentException.php @@ -0,0 +1,25 @@ +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()); } }