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 @@ 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..ab67e41a9b 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']); + $this->getDatabase()->insert('movies', ['_id' => 999]); + } + + public function testExceedTimeException() + { + self::expectException(Exception::class); + self::expectExceptionCode(50); + + $this->getDatabase()->find( + 'movies', + ['$where' => 'sleep(1000) || true'], + ['maxTimeMS'=> 1] + )->cursor->firstBatch ?? []; } }