diff --git a/src/Client.php b/src/Client.php index 562d2d7a12..45a8e9a6ae 100644 --- a/src/Client.php +++ b/src/Client.php @@ -202,14 +202,21 @@ class Client $result = BSON\toPHP(substr($res, 21, $responseLength - 21)); if (property_exists($result, "writeErrors")) { - // Throws a Utopia\Mongo\Exception with Code Error - + // Throws Utopia\Mongo\Exception throw new Exception( $result->writeErrors[0]->errmsg, $result->writeErrors[0]->code ); } + if (property_exists($result, 'errmsg')) { + // Throws Utopia\Mongo\Exception + throw new Exception( + 'E'.$result->code.' '.$result->codeName.':'.$result->errmsg, + $result->code + ); + } + if (property_exists($result, "n") && $result->ok === 1.0) { return $result->n; } @@ -218,10 +225,6 @@ class Client return $result; } - if (property_exists($result, 'errmsg')) { - throw new Exception($result->errmsg); - } - if ($result->ok === 1.0) { return $result; } diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 7434d782b2..5765a4ef3d 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -106,9 +106,31 @@ class MongoTest extends TestCase self::assertIsArray($doc->array); self::assertIsString($doc->date_string); self::assertIsObject($doc->date_object); // Todo: This is not working can't retrieve the object back + } + public function testDriverException() + { + self::expectException('MongoDB\Driver\Exception\InvalidArgumentException'); + self::expectExceptionCode(0); + new ObjectId('triggerAnError'); + } + + public function testDuplicationException() + { self::expectException(Exception::class); self::expectExceptionCode(11000); $this->getDatabase()->insert('movies', ['_id' => 999, 'b' => 'Duplication']); } + + public function testExceedTimeException() + { + self::expectException(Exception::class); + self::expectExceptionCode(50); + + $this->getDatabase()->find( + 'movies', + ['$where' => "sleep(1000) || true"], + ['maxTimeMS'=> 1] + )->cursor->firstBatch ?? []; + } }