Skip to content

Commit

Permalink
Merge pull request #22 from keboola/adamvyborny-CM-632-handle-unautho…
Browse files Browse the repository at this point in the history
…rized-as-user-exception

Handle unauthorized as user exception
AdamVyborny authored Jul 19, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents dbfea38 + d29fe2e commit 2ad8c33
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Export.php
Original file line number Diff line number Diff line change
@@ -137,6 +137,12 @@ protected function handleMongoExportFails(Throwable $e): void
throw new UserException('$sort key ordering must be 1 (for ascending) or -1 (for descending)');
}

if (preg_match('/(Failed:.*?command)/s', $e->getMessage(), $matches)) {
if (isset($matches[1])) {
throw new UserException(trim($matches[1]));
}
}

if (preg_match('/query \'\\[[^\\]]*\\]\' is not valid JSON/i', $e->getMessage())) {
throw new UserException(sprintf(
'Export "%s" failed. Query "' . $this->exportOptions->getQuery() . '" is not valid JSON',
13 changes: 13 additions & 0 deletions tests/phpunit/HandleMongoExportFailsTest.php
Original file line number Diff line number Diff line change
@@ -66,6 +66,19 @@ public function exceptionsProvider(): Generator
'Failed: (Location15975) $sort key ordering must be 1 (for ascending) or -1 (for descending)')),
new UserException('$sort key ordering must be 1 (for ascending) or -1 (for descending)'),
];

yield 'Unauthorized' => [
new ProcessFailedException($this->createMockInstanceOfProcess('2023-05-11T00:07:31.097+0000\t' .
'connected to: mongodb+srv://[**REDACTED**]@cl-shared-all-dev-web.3ebye.mongodb.net/slotManagementDB' .
'Test 2023-05-11T00:07:31.178+0000\tFailed: (Unauthorized) not authorized on slotManagementDBTest to ' .
'execute command { aggregate: \"settings\", cursor: {}, pipeline: [ { $collStats: { count: {} } }, { ' .
'$group: { _id: 1, n: { $sum: \"$count\" } } } ], lsid: { id: UUID(\"7a85274b-06b1-4314-b625-25b0a022' .
'2454\") }, $clusterTime: { clusterTime: Timestamp(1683763642, 1), signature: { hash: BinData(0, 4F8D' .
'EAC4648CBF6050C6CC7A397F6A0FAFC277EC), keyId: 7218577901191430149 } }, $db: \"slotManagementDBTest\"' .
' $readPreference: { mode: \"primary\" } }')),
new UserException('Failed: (Unauthorized) not authorized on slotManagementDBTest to ' .
'execute command'),
];
}

private function createMockInstanceOfProcess(string $errorOutput): Process

0 comments on commit 2ad8c33

Please sign in to comment.