Skip to content

Commit 20be183

Browse files
committed
Improve exception when user tries to open dir
1 parent 0d82c28 commit 20be183

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
1.12.0
5+
-------------------
6+
7+
* Improve the error handling when the user tries to open a directory
8+
with the pure PHP reader.
9+
410
1.11.1 (2023-12-01)
511
-------------------
612

src/MaxMind/Db/Reader.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ public function __construct(string $database)
7979
);
8080
}
8181

82+
if (is_dir($database)) {
83+
// This matches the error that the C extension throws.
84+
throw new InvalidDatabaseException(
85+
"Error opening database file ($database). Is this a valid MaxMind DB file?"
86+
);
87+
}
88+
8289
$fileHandle = @fopen($database, 'rb');
8390
if ($fileHandle === false) {
8491
throw new \InvalidArgumentException(

tests/MaxMind/Db/Test/ReaderTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,13 @@ public function testNonDatabase(): void
305305
new Reader('README.md');
306306
}
307307

308+
public function testOpeningDir(): void
309+
{
310+
$this->expectException(InvalidDatabaseException::class);
311+
$this->expectExceptionMessage('Error opening database file (tests/). Is this a valid MaxMind DB file?');
312+
new Reader('tests/');
313+
}
314+
308315
public function testTooManyConstructorArgs(): void
309316
{
310317
$this->expectException(\ArgumentCountError::class);

0 commit comments

Comments
 (0)