Skip to content

Commit 51bee37

Browse files
committed
Improve error message for invalid configuration
1 parent 390c9ec commit 51bee37

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55

66
* Fix memory leak when filling nested fields using dot notation by @GromNaN in [#2962](https://github.com/mongodb/laravel-mongodb/pull/2962)
77
* Fix PHP error when accessing the connection after disconnect by @SanderMuller in [#2967](https://github.com/mongodb/laravel-mongodb/pull/2967)
8+
* Improve error message for invalid configuration by @GromNaN in [#2975](https://github.com/mongodb/laravel-mongodb/pull/2975)
89

910
## [4.3.0] - 2024-04-26
1011

src/Connection.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,15 @@ protected function getHostDsn(array $config): string
261261
*/
262262
protected function getDsn(array $config): string
263263
{
264-
return $this->hasDsnString($config)
265-
? $this->getDsnString($config)
266-
: $this->getHostDsn($config);
264+
if (! empty($config['dsn'])) {
265+
return $this->getDsnString($config);
266+
}
267+
268+
if (! empty($config['host'])) {
269+
return $this->getHostDsn($config);
270+
}
271+
272+
throw new InvalidArgumentException('MongoDB connection configuration requires "dsn" or "host" key.');
267273
}
268274

269275
/** @inheritdoc */

tests/ConnectionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ public function testConnectionWithoutConfiguredDatabase(): void
204204
new Connection(['dsn' => 'mongodb://some-host']);
205205
}
206206

207+
public function testConnectionWithoutConfiguredDsnOrHost(): void
208+
{
209+
$this->expectException(InvalidArgumentException::class);
210+
$this->expectExceptionMessage('MongoDB connection configuration requires "dsn" or "host" key.');
211+
212+
new Connection(['database' => 'hello']);
213+
}
214+
207215
public function testCollection()
208216
{
209217
$collection = DB::connection('mongodb')->getCollection('unittest');

0 commit comments

Comments
 (0)