From 7e948a82ad438be9385e0b8d5822479ed1f94da3 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 14 Dec 2022 13:14:48 +0200 Subject: [PATCH 1/5] second version error exeption --- src/Client.php | 15 +++++++++------ tests/MongoTest.php | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) 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 ?? []; + } } From 765acfe17f1c7518dbfb2c90d659412f73f3e9fb Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 14 Dec 2022 13:15:48 +0200 Subject: [PATCH 2/5] second version error Exception --- tests/MongoTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 5765a4ef3d..2a3d2f1c11 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -119,7 +119,7 @@ class MongoTest extends TestCase { self::expectException(Exception::class); self::expectExceptionCode(11000); - $this->getDatabase()->insert('movies', ['_id' => 999, 'b' => 'Duplication']); + $this->getDatabase()->insert('movies', ['_id' => 999]); } public function testExceedTimeException() From bfe82cfe9e4d71b6f02b35d9236f1d62ebe3526c Mon Sep 17 00:00:00 2001 From: Shmuel Fogel Date: Mon, 19 Dec 2022 09:08:14 +0200 Subject: [PATCH 3/5] Update tests/MongoTest.php Co-authored-by: Jake Barnby --- tests/MongoTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/MongoTest.php b/tests/MongoTest.php index 2a3d2f1c11..508c1e6b2b 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -129,7 +129,7 @@ class MongoTest extends TestCase $this->getDatabase()->find( 'movies', - ['$where' => "sleep(1000) || true"], + ['$where' => 'sleep(1000) || true'], ['maxTimeMS'=> 1] )->cursor->firstBatch ?? []; } From 570f920bdfa3cdd8f83a8109ed5b2468eb4ec979 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 21 Dec 2022 09:26:22 +0200 Subject: [PATCH 4/5] Adding space --- src/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Client.php b/src/Client.php index 45a8e9a6ae..ececad20b0 100644 --- a/src/Client.php +++ b/src/Client.php @@ -212,7 +212,7 @@ class Client if (property_exists($result, 'errmsg')) { // Throws Utopia\Mongo\Exception throw new Exception( - 'E'.$result->code.' '.$result->codeName.':'.$result->errmsg, + 'E'.$result->code.' '.$result->codeName.': '.$result->errmsg, $result->code ); } From 7a340cb8f80f6e69ea4feaf5f790079ca61a7071 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 21 Dec 2022 11:29:46 +0200 Subject: [PATCH 5/5] comments adapters since they are not in use --- src/Adapter/SwooleClient.php | 53 ++++++++++++++------------- src/Adapter/SwooleCoroutineClient.php | 53 ++++++++++++++------------- tests/MongoTest.php | 8 ++-- 3 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/Adapter/SwooleClient.php b/src/Adapter/SwooleClient.php index c1a86f24ac..e5121c8c00 100644 --- a/src/Adapter/SwooleClient.php +++ b/src/Adapter/SwooleClient.php @@ -1,28 +1,29 @@ getDatabase()->find( - 'movies', - ['$where' => 'sleep(1000) || true'], - ['maxTimeMS'=> 1] - )->cursor->firstBatch ?? []; + 'movies', + ['$where' => 'sleep(1000) || true'], + ['maxTimeMS'=> 1] + )->cursor->firstBatch ?? []; } }