Skip to content
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
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This serves two purposes:
- You can now specify navigation priorities by adding a numeric prefix to the source file names in https://github.com/hydephp/develop/pull/1709
- Added a new `\Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets` build task handle media assets transfers for site builds.
- 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
- Added a new `\Hyde\Framework\Exceptions\InvalidConfigurationException` exception class to handle invalid configuration exceptions in https://github.com/hydephp/develop/pull/1799
- The `\Hyde\Facades\Features` class is no longer marked as internal, and is now thus part of the public API.

### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Exceptions;

use Hyde\Facades\Filesystem;
use InvalidArgumentException;

use function assert;
use function explode;
use function realpath;

class InvalidConfigurationException extends InvalidArgumentException
{
public function __construct(string $message = 'Invalid configuration detected.', ?string $namespace = null, ?string $key = null)
{
if ($namespace && $key) {
[$this->file, $this->line] = $this->findConfigLine($namespace, $key);
}

parent::__construct($message);
}

/**
* @experimental Please report any issues with this method to the authors at https://github.com/hydephp/develop/issues
*
* @return array{string, int}
*/
protected function findConfigLine(string $namespace, string $key): array
{
$file = realpath("config/$namespace.php");
$contents = Filesystem::getContents($file);
$lines = explode("\n", $contents);

foreach ($lines as $line => $content) {
if (str_contains($content, "'$key' =>")) {
break;
}
}

assert($file !== false);
assert(isset($line));

return [$file, $line + 1];
}
}
28 changes: 28 additions & 0 deletions packages/framework/tests/Unit/CustomExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
use Hyde\Framework\Exceptions\ParseException;
use RuntimeException;
use Exception;
use Hyde\Framework\Exceptions\InvalidConfigurationException;

/**
* @covers \Hyde\Framework\Exceptions\FileConflictException
* @covers \Hyde\Framework\Exceptions\FileNotFoundException
* @covers \Hyde\Framework\Exceptions\RouteNotFoundException
* @covers \Hyde\Framework\Exceptions\UnsupportedPageTypeException
* @covers \Hyde\Framework\Exceptions\InvalidConfigurationException
* @covers \Hyde\Framework\Exceptions\ParseException
*/
class CustomExceptionsTest extends UnitTestCase
Expand Down Expand Up @@ -173,4 +175,30 @@ public function testParseExceptionWithPrevious()
$this->assertSame("Invalid Markdown in file: 'example.md' (Parsing error)", $exception->getMessage());
$this->assertSame($previous, $exception->getPrevious());
}

public function testInvalidConfigurationExceptionWithDefaultMessage()
{
$exception = new InvalidConfigurationException();

$this->assertSame('Invalid configuration detected.', $exception->getMessage());
}

public function testInvalidConfigurationExceptionWithCustomMessage()
{
$exception = new InvalidConfigurationException('Custom error message.');

$this->assertSame('Custom error message.', $exception->getMessage());
}

public function testInvalidConfigurationExceptionWithNamespaceAndKey()
{
$exception = new InvalidConfigurationException('Invalid configuration.', 'hyde', 'name');

$this->assertSame('Invalid configuration.', $exception->getMessage());
$this->assertFileExists($exception->getFile());
$this->assertIsInt($exception->getLine());

$this->assertStringContainsString('config'.DIRECTORY_SEPARATOR.'hyde.php', $exception->getFile());
$this->assertGreaterThan(0, $exception->getLine());
}
}