Skip to content

Commit f58a07b

Browse files
committed
Extract experimental helper method to try parsing config values
1 parent 997a6ee commit f58a07b

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

packages/framework/src/Foundation/Internal/LoadYamlConfiguration.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,8 @@ protected function mergeConfiguration(string $namespace, array $yaml): void
6767
protected function parseAuthors(array $authors): array
6868
{
6969
return Arr::mapWithKeys($authors, function (array $author, string $username): array {
70-
try {
71-
return [$username => PostAuthor::create($author)];
72-
} catch (Throwable $exception) {
73-
throw new InvalidConfigurationException(
74-
'Invalid author configuration detected in the YAML config file. Please double check the syntax.',
75-
previous: $exception
76-
);
77-
}
70+
$message = 'Invalid author configuration detected in the YAML config file. Please double check the syntax.';
71+
return InvalidConfigurationException::try(fn ()=> [$username => PostAuthor::create($author)], $message);
7872
});
7973
}
8074
}

packages/framework/src/Framework/Exceptions/InvalidConfigurationException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ protected function findConfigLine(string $namespace, string $key): array
4545

4646
return [$file, $line + 1];
4747
}
48+
49+
/**
50+
* @internal
51+
* @experimental
52+
*/
53+
public static function try(callable $callback, ?string $message = null): mixed
54+
{
55+
try {
56+
return $callback();
57+
} catch (Throwable $exception) {
58+
throw new static($message ?? $exception->getMessage(), previous: $exception);
59+
}
60+
}
4861
}

packages/framework/src/Framework/Features/Navigation/NavigationMenuGenerator.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,9 @@ protected function generate(): void
8585
} else {
8686
collect(Config::getArray('hyde.navigation.custom', []))->each(function (array $data): void {
8787
/** @var array{destination: string, label: ?string, priority: ?int, attributes: array<string, scalar>} $data */
88-
try {
89-
$item = NavigationItem::create(...$data);
90-
} catch (Throwable $exception) {
91-
throw new InvalidConfigurationException(
92-
'Invalid navigation item configuration detected the configuration file. Please double check the syntax.',
93-
previous: $exception
94-
);
95-
}
88+
89+
$message = 'Invalid navigation item configuration detected the configuration file. Please double check the syntax.';
90+
$item = InvalidConfigurationException::try(fn () => NavigationItem::create(...$data), $message);
9691

9792
// Since these were added explicitly by the user, we can assume they should always be shown
9893
$this->items->push($item);

packages/framework/tests/Unit/CustomExceptionsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,45 @@ public function testInvalidConfigurationExceptionWithPreviousThrowable()
210210
$this->assertSame('Invalid configuration.', $exception->getMessage());
211211
$this->assertSame($previous, $exception->getPrevious());
212212
}
213+
214+
public function testInvalidConfigurationExceptionTryMethodWithSuccessfulCallback()
215+
{
216+
$result = InvalidConfigurationException::try(function () {
217+
return 'success';
218+
});
219+
220+
$this->assertSame('success', $result);
221+
}
222+
223+
public function testInvalidConfigurationExceptionTryMethodWithThrowingCallback()
224+
{
225+
$this->expectException(InvalidConfigurationException::class);
226+
$this->expectExceptionMessage('Custom error message');
227+
228+
InvalidConfigurationException::try(function () {
229+
throw new RuntimeException('Original error');
230+
}, 'Custom error message');
231+
}
232+
233+
public function testInvalidConfigurationExceptionTryMethodWithDefaultMessage()
234+
{
235+
$this->expectException(InvalidConfigurationException::class);
236+
$this->expectExceptionMessage('Original error');
237+
238+
InvalidConfigurationException::try(function () {
239+
throw new RuntimeException('Original error');
240+
});
241+
}
242+
243+
public function testInvalidConfigurationExceptionTryMethodPreservesPreviousException()
244+
{
245+
try {
246+
InvalidConfigurationException::try(function () {
247+
throw new RuntimeException('Original error');
248+
}, 'Custom error message');
249+
} catch (InvalidConfigurationException $e) {
250+
$this->assertInstanceOf(RuntimeException::class, $e->getPrevious());
251+
$this->assertSame('Original error', $e->getPrevious()->getMessage());
252+
}
253+
}
213254
}

0 commit comments

Comments
 (0)