Skip to content

Commit 64ba1fb

Browse files
committed
Add external error handling support and rename "add" method "register"
1 parent 17c9b40 commit 64ba1fb

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

Diff for: src/Factory.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace TypeLang\PHPDoc;
66

7+
use TypeLang\PHPDoc\Exception\InvalidTagException;
8+
use TypeLang\PHPDoc\Exception\ParsingException;
9+
use TypeLang\PHPDoc\Exception\RuntimeExceptionInterface;
710
use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface;
811
use TypeLang\PHPDoc\Tag\Tag;
912

@@ -16,7 +19,7 @@ public function __construct(
1619
private array $factories = [],
1720
) {}
1821

19-
public function add(array|string $tags, FactoryInterface $delegate): void
22+
public function register(array|string $tags, FactoryInterface $delegate): void
2023
{
2124
foreach ((array) $tags as $tag) {
2225
$this->factories[$tag] = $delegate;
@@ -28,7 +31,24 @@ public function create(string $name, string $content, DescriptionParserInterface
2831
$delegate = $this->factories[$name] ?? null;
2932

3033
if ($delegate !== null) {
31-
return $delegate->create($name, $content, $descriptions);
34+
try {
35+
return $delegate->create($name, $content, $descriptions);
36+
} catch (ParsingException $e) {
37+
throw $e;
38+
} catch (RuntimeExceptionInterface $e) {
39+
throw InvalidTagException::fromCreatingTag(
40+
tag: $name,
41+
source: $e->getSource(),
42+
offset: $e->getOffset(),
43+
prev: $e,
44+
);
45+
} catch (\Throwable $e) {
46+
throw InvalidTagException::fromCreatingTag(
47+
tag: $name,
48+
source: $content,
49+
prev: $e,
50+
);
51+
}
3252
}
3353

3454
return new Tag($name, $descriptions->parse($content));

Diff for: src/FactoryInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace TypeLang\PHPDoc;
66

7+
use TypeLang\PHPDoc\Exception\RuntimeExceptionInterface;
78
use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface;
89
use TypeLang\PHPDoc\Tag\Tag;
910

@@ -13,6 +14,9 @@ interface FactoryInterface
1314
* Returns a tag object with the specified name and description.
1415
*
1516
* @param non-empty-string $name
17+
*
18+
* @throws RuntimeExceptionInterface In case of parsing error occurs.
19+
* @throws \Throwable In case of internal error occurs.
1620
*/
1721
public function create(string $name, string $content, DescriptionParserInterface $descriptions): Tag;
1822
}

Diff for: src/MutableFactoryInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ interface MutableFactoryInterface extends FactoryInterface
99
/**
1010
* @param non-empty-string|list<non-empty-string> $tags
1111
*/
12-
public function add(string|array $tags, FactoryInterface $delegate): void;
12+
public function register(string|array $tags, FactoryInterface $delegate): void;
1313
}

0 commit comments

Comments
 (0)