Skip to content

Commit

Permalink
Improve exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Oct 26, 2023
1 parent 84b72e4 commit bb247ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/Exception/NonPrintableNodeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace TypeLang\Printer\Exception;

use TypeLang\Parser\Node\Node;

class NonPrintableNodeException extends \InvalidArgumentException implements PrinterExceptionInterface
{
final public const CODE_INVALID_NODE = 0x01;

public const CODE_LAST = self::CODE_INVALID_NODE;

final public function __construct(string $message, int $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

public static function fromInvalidNode(Node $node): self
{
$message = \sprintf('Could not print unknown node "%s"', $node::class);

return new static($message, self::CODE_INVALID_NODE);
}
}
9 changes: 9 additions & 0 deletions src/Exception/PrinterExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace TypeLang\Printer\Exception;

interface PrinterExceptionInterface extends \Throwable
{
}
1 change: 0 additions & 1 deletion src/NativeTypePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace TypeLang\Printer;

use TypeLang\Parser\Node\Statement;
use TypeLang\Parser\Node\Type\CallableTypeNode;
use TypeLang\Parser\Node\Type\ClassConstMaskNode;
use TypeLang\Parser\Node\Type\ClassConstNode;
Expand Down
5 changes: 2 additions & 3 deletions src/PrettyPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use TypeLang\Parser\Node\Type\TypeStatement;
use TypeLang\Parser\Node\Type\UnionTypeNode;
use TypeLang\Parser\Traverser;
use TypeLang\Printer\Exception\NonPrintableNodeException;

class PrettyPrinter extends Printer
{
Expand Down Expand Up @@ -102,9 +103,7 @@ protected function make(Statement $stmt): string
$stmt instanceof UnionTypeNode => $this->printUnionTypeNode($stmt),
$stmt instanceof IntersectionTypeNode => $this->printIntersectionTypeNode($stmt),
$stmt instanceof NullableTypeNode => $this->printNullableType($stmt),
default => throw new \InvalidArgumentException(
\sprintf('Non-printable node "%s"', $stmt::class),
),
default => throw NonPrintableNodeException::fromInvalidNode($stmt),
};
}

Expand Down

0 comments on commit bb247ff

Please sign in to comment.