Skip to content

Commit

Permalink
Merge pull request #174 from maxmind/greg/better-dir-error
Browse files Browse the repository at this point in the history
Improve exception when user tries to open dir
  • Loading branch information
shadromani authored Oct 10, 2024
2 parents 0d82c28 + 20be183 commit 87906bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

1.12.0
-------------------

* Improve the error handling when the user tries to open a directory
with the pure PHP reader.

1.11.1 (2023-12-01)
-------------------

Expand Down
7 changes: 7 additions & 0 deletions src/MaxMind/Db/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public function __construct(string $database)
);
}

if (is_dir($database)) {
// This matches the error that the C extension throws.
throw new InvalidDatabaseException(
"Error opening database file ($database). Is this a valid MaxMind DB file?"
);
}

$fileHandle = @fopen($database, 'rb');
if ($fileHandle === false) {
throw new \InvalidArgumentException(
Expand Down
7 changes: 7 additions & 0 deletions tests/MaxMind/Db/Test/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ public function testNonDatabase(): void
new Reader('README.md');
}

public function testOpeningDir(): void
{
$this->expectException(InvalidDatabaseException::class);
$this->expectExceptionMessage('Error opening database file (tests/). Is this a valid MaxMind DB file?');
new Reader('tests/');
}

public function testTooManyConstructorArgs(): void
{
$this->expectException(\ArgumentCountError::class);
Expand Down

0 comments on commit 87906bb

Please sign in to comment.