From 9690100a732369b590ba400e2c254446123a8c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Thu, 24 Aug 2023 22:12:01 +0200 Subject: [PATCH] Handle field names may not start with $ as user exception --- src/Export.php | 4 ++++ tests/phpunit/HandleMongoExportFailsTest.php | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/Export.php b/src/Export.php index ded496b..c84b643 100644 --- a/src/Export.php +++ b/src/Export.php @@ -137,6 +137,10 @@ protected function handleMongoExportFails(Throwable $e): void throw new UserException('$sort key ordering must be 1 (for ascending) or -1 (for descending)'); } + if (str_contains($e->getMessage(), 'FieldPath field names may not start with \'$\'')) { + throw new UserException('FieldPath field names may not start with \'$\''); + } + if (preg_match('/(Failed:.*?command)/s', $e->getMessage(), $matches)) { if (isset($matches[1])) { throw new UserException(trim($matches[1])); diff --git a/tests/phpunit/HandleMongoExportFailsTest.php b/tests/phpunit/HandleMongoExportFailsTest.php index a9f1f95..c9f4aee 100644 --- a/tests/phpunit/HandleMongoExportFailsTest.php +++ b/tests/phpunit/HandleMongoExportFailsTest.php @@ -79,6 +79,14 @@ public function exceptionsProvider(): Generator new UserException('Failed: (Unauthorized) not authorized on slotManagementDBTest to ' . 'execute command'), ]; + + yield 'field names may not start with $' => [ + new ProcessFailedException($this->createMockInstanceOfProcess('2023-05-17T12:49:22.079+0000\t' . + 'connected to: mongodb+srv://[**REDACTED**]@cl-shared-all-prod-web.x0u5m.mongodb.net/slotManagementDB' . + '2023-05-17T12:49:22.238+0000\tFailed: (Location16410) FieldPath field names may not start with \'$\'' . + 'Consider using $getField or $setField.')), + new UserException('FieldPath field names may not start with \'$\''), + ]; } private function createMockInstanceOfProcess(string $errorOutput): Process