Skip to content

Commit 4559d0c

Browse files
authored
Merge pull request #1799 from hydephp/create-invalid-configuration-exception-class
[2.x] Add an invalid configuration exception class
2 parents 63675c7 + f8c8847 commit 4559d0c

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This serves two purposes:
1313
- You can now specify navigation priorities by adding a numeric prefix to the source file names in https://github.com/hydephp/develop/pull/1709
1414
- Added a new `\Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets` build task handle media assets transfers for site builds.
1515
- Added a new `\Hyde\Framework\Exceptions\ParseException` exception class to handle parsing exceptions in data collection files in https://github.com/hydephp/develop/pull/1732
16+
- Added a new `\Hyde\Framework\Exceptions\InvalidConfigurationException` exception class to handle invalid configuration exceptions in https://github.com/hydephp/develop/pull/1799
1617
- The `\Hyde\Facades\Features` class is no longer marked as internal, and is now thus part of the public API.
1718

1819
### Changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hyde\Framework\Exceptions;
6+
7+
use Hyde\Facades\Filesystem;
8+
use InvalidArgumentException;
9+
10+
use function assert;
11+
use function explode;
12+
use function realpath;
13+
14+
class InvalidConfigurationException extends InvalidArgumentException
15+
{
16+
public function __construct(string $message = 'Invalid configuration detected.', ?string $namespace = null, ?string $key = null)
17+
{
18+
if ($namespace && $key) {
19+
[$this->file, $this->line] = $this->findConfigLine($namespace, $key);
20+
}
21+
22+
parent::__construct($message);
23+
}
24+
25+
/**
26+
* @experimental Please report any issues with this method to the authors at https://github.com/hydephp/develop/issues
27+
*
28+
* @return array{string, int}
29+
*/
30+
protected function findConfigLine(string $namespace, string $key): array
31+
{
32+
$file = realpath("config/$namespace.php");
33+
$contents = Filesystem::getContents($file);
34+
$lines = explode("\n", $contents);
35+
36+
foreach ($lines as $line => $content) {
37+
if (str_contains($content, "'$key' =>")) {
38+
break;
39+
}
40+
}
41+
42+
assert($file !== false);
43+
assert(isset($line));
44+
45+
return [$file, $line + 1];
46+
}
47+
}

packages/framework/tests/Unit/CustomExceptionsTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
use Hyde\Framework\Exceptions\ParseException;
1414
use RuntimeException;
1515
use Exception;
16+
use Hyde\Framework\Exceptions\InvalidConfigurationException;
1617

1718
/**
1819
* @covers \Hyde\Framework\Exceptions\FileConflictException
1920
* @covers \Hyde\Framework\Exceptions\FileNotFoundException
2021
* @covers \Hyde\Framework\Exceptions\RouteNotFoundException
2122
* @covers \Hyde\Framework\Exceptions\UnsupportedPageTypeException
23+
* @covers \Hyde\Framework\Exceptions\InvalidConfigurationException
2224
* @covers \Hyde\Framework\Exceptions\ParseException
2325
*/
2426
class CustomExceptionsTest extends UnitTestCase
@@ -173,4 +175,30 @@ public function testParseExceptionWithPrevious()
173175
$this->assertSame("Invalid Markdown in file: 'example.md' (Parsing error)", $exception->getMessage());
174176
$this->assertSame($previous, $exception->getPrevious());
175177
}
178+
179+
public function testInvalidConfigurationExceptionWithDefaultMessage()
180+
{
181+
$exception = new InvalidConfigurationException();
182+
183+
$this->assertSame('Invalid configuration detected.', $exception->getMessage());
184+
}
185+
186+
public function testInvalidConfigurationExceptionWithCustomMessage()
187+
{
188+
$exception = new InvalidConfigurationException('Custom error message.');
189+
190+
$this->assertSame('Custom error message.', $exception->getMessage());
191+
}
192+
193+
public function testInvalidConfigurationExceptionWithNamespaceAndKey()
194+
{
195+
$exception = new InvalidConfigurationException('Invalid configuration.', 'hyde', 'name');
196+
197+
$this->assertSame('Invalid configuration.', $exception->getMessage());
198+
$this->assertFileExists($exception->getFile());
199+
$this->assertIsInt($exception->getLine());
200+
201+
$this->assertStringContainsString('config'.DIRECTORY_SEPARATOR.'hyde.php', $exception->getFile());
202+
$this->assertGreaterThan(0, $exception->getLine());
203+
}
176204
}

0 commit comments

Comments
 (0)