Skip to content

fix(mongodb): Exception throwing fatal error, Broken handling of Mong… #1032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions pkg/mongodb/MongodbConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MongodbConnectionFactory implements ConnectionFactory
*
* or
*
* mongodb://127.0.0.1:27017/dbname?polling_interval=1000&enqueue_collection=enqueue
* mongodb://127.0.0.1:27017/defaultauthdb?polling_interval=1000&enqueue_database=enqueue&enqueue_collection=enqueue
*
* @param array|string|null $config
*/
Expand All @@ -38,7 +38,10 @@ public function __construct($config = 'mongodb:')
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
$config = $this->parseDsn(empty($config['dsn']) ? 'mongodb:' : $config['dsn']);
$config = array_replace(
$config,
$this->parseDsn(empty($config['dsn']) ? 'mongodb:' : $config['dsn'])
);
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
Expand Down Expand Up @@ -74,18 +77,16 @@ public static function parseDsn(string $dsn): array
'mongodb' => true,
];
if (false == isset($parsedUrl['scheme'])) {
throw new \LogicException(sprintf(
'The given DSN schema "%s" is not supported. There are supported schemes: "%s".',
$parsedUrl['scheme'],
implode('", "', array_keys($supported))
));
throw new \LogicException(sprintf('The given DSN schema "%s" is not supported. There are supported schemes: "%s".', $parsedUrl['scheme'], implode('", "', array_keys($supported))));
}
if ('mongodb:' === $dsn) {
return [
'dsn' => 'mongodb://127.0.0.1/',
];
}
$config['dsn'] = $dsn;
// FIXME this is NOT a dbname but rather authdb. But removing this would be a BC break.
// see: https://github.com/php-enqueue/enqueue-dev/issues/1027
if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) {
$pathParts = explode('/', $parsedUrl['path']);
//DB name
Expand All @@ -103,6 +104,9 @@ public static function parseDsn(string $dsn): array
if (!empty($queryParts['enqueue_collection'])) {
$config['collection_name'] = $queryParts['enqueue_collection'];
}
if (!empty($queryParts['enqueue_database'])) {
$config['dbname'] = $queryParts['enqueue_database'];
}
}

return $config;
Expand Down
12 changes: 3 additions & 9 deletions pkg/mongodb/MongodbProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ public function send(Destination $destination, Message $message): void
$delay = $message->getDeliveryDelay();
if ($delay) {
if (!is_int($delay)) {
throw new \LogicException(sprintf(
'Delay must be integer but got: "%s"',
is_object($delay) ? get_class($delay) : gettype($delay)
));
throw new \LogicException(sprintf('Delay must be integer but got: "%s"', is_object($delay) ? get_class($delay) : gettype($delay)));
}

if ($delay <= 0) {
Expand All @@ -93,10 +90,7 @@ public function send(Destination $destination, Message $message): void
$timeToLive = $message->getTimeToLive();
if ($timeToLive) {
if (!is_int($timeToLive)) {
throw new \LogicException(sprintf(
'TimeToLive must be integer but got: "%s"',
is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive)
));
throw new \LogicException(sprintf('TimeToLive must be integer but got: "%s"', is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive)));
}

if ($timeToLive <= 0) {
Expand All @@ -110,7 +104,7 @@ public function send(Destination $destination, Message $message): void
$collection = $this->context->getCollection();
$collection->insertOne($mongoMessage);
} catch (\Exception $e) {
throw new Exception('The transport has failed to send the message due to some internal error.', null, $e);
throw new Exception('The transport has failed to send the message due to some internal error.', $e->getCode(), $e);
}
}

Expand Down