Skip to content

Commit 9881bfa

Browse files
committed
Improve printer exceptions
1 parent 199a643 commit 9881bfa

4 files changed

Lines changed: 14 additions & 15 deletions

File tree

libs/printer/src/Exception/NonPrintableNodeException.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,12 @@
66

77
use TypeLang\Type\Node;
88

9-
class NonPrintableNodeException extends \InvalidArgumentException implements PrinterExceptionInterface
9+
final class NonPrintableNodeException extends PrinterException
1010
{
11-
final public const ERROR_CODE_INVALID_NODE = 0x01;
12-
13-
protected const CODE_LAST = self::ERROR_CODE_INVALID_NODE;
14-
15-
final public function __construct(string $message, int $code = 0, ?\Throwable $previous = null)
16-
{
17-
parent::__construct($message, $code, $previous);
18-
}
19-
20-
public static function fromInvalidNode(Node $node): self
11+
public static function becauseInvalidNodeGiven(Node $node, ?\Throwable $previous = null): self
2112
{
2213
$message = \sprintf('Could not print unknown node "%s"', $node::class);
2314

24-
return new static($message, self::ERROR_CODE_INVALID_NODE);
15+
return new self($message, 0, $previous);
2516
}
2617
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Printer\Exception;
6+
7+
abstract class PrinterException extends \RuntimeException implements
8+
PrinterExceptionInterface {}

libs/printer/src/NativeTypePrinter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected function printCondition(Condition $node): string
118118
return match (true) {
119119
$node instanceof EqualConditionNode => '===',
120120
$node instanceof NotEqualConditionNode => '!==',
121-
default => throw NonPrintableNodeException::fromInvalidNode($node),
121+
default => throw NonPrintableNodeException::becauseInvalidNodeGiven($node),
122122
};
123123
}
124124

libs/printer/src/PrettyPrinter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function make(TypeNode $stmt): string
138138
$stmt instanceof TernaryExpressionNode => $this->printTernaryType($stmt),
139139
$stmt instanceof TypesListNode => $this->printTypeListNode($stmt),
140140
$stmt instanceof TypeOffsetAccessNode => $this->printTypeOffsetAccessNode($stmt),
141-
default => throw NonPrintableNodeException::fromInvalidNode($stmt),
141+
default => throw NonPrintableNodeException::becauseInvalidNodeGiven($stmt),
142142
};
143143
}
144144

@@ -567,7 +567,7 @@ protected function printCondition(Condition $node): string
567567
$node instanceof LessThanOrEqualConditionNode => '<=',
568568
$node instanceof GreaterThanConditionNode => '>',
569569
$node instanceof LessThanConditionNode => '<',
570-
default => throw NonPrintableNodeException::fromInvalidNode($node),
570+
default => throw NonPrintableNodeException::becauseInvalidNodeGiven($node),
571571
};
572572
}
573573

0 commit comments

Comments
 (0)