diff --git a/src/Application.php b/src/Application.php index d77de21..fd3defe 100644 --- a/src/Application.php +++ b/src/Application.php @@ -4,6 +4,7 @@ namespace Aeliot\TodoRegistrar; +use Aeliot\TodoRegistrar\Exception\CommentRegistrationException; use Aeliot\TodoRegistrar\Service\File\Finder; use Aeliot\TodoRegistrar\Service\FileProcessor; @@ -18,7 +19,31 @@ public function __construct( public function run(): void { foreach ($this->finder as $file) { - $this->fileProcessor->process($file); + try { + $this->fileProcessor->process($file); + } catch (\Throwable $exception) { + $this->writeError($exception, $file); + } } } + + private function writeError(\Throwable $exception, ?\SplFileInfo $file = null): void + { + $previousException = $exception->getPrevious(); + if ($previousException) { + $this->writeError($previousException); + } + + $message = "[ERROR] {$exception->getMessage()} on {$exception->getFile()}:{$exception->getLine()}"; + if ($file) { + $message .= ". Cannot process file: {$file->getPathname()}"; + } + if ($exception instanceof CommentRegistrationException) { + $message .= " with comment on line {$exception->getToken()->line}"; + $message .= " and obtained text: {$exception->getCommentPart()->getContent()}"; + } + $message .= "\n"; + + fwrite(STDERR, $message); + } } \ No newline at end of file diff --git a/src/Exception/CommentRegistrationException.php b/src/Exception/CommentRegistrationException.php new file mode 100644 index 0000000..8535070 --- /dev/null +++ b/src/Exception/CommentRegistrationException.php @@ -0,0 +1,28 @@ +commentPart->getTag()), 0, $previous); + } + + public function getCommentPart(): CommentPart + { + return $this->commentPart; + } + + public function getToken(): \PhpToken + { + return $this->token; + } +} diff --git a/src/Service/CommentRegistrar.php b/src/Service/CommentRegistrar.php index 128c464..91fdbe2 100644 --- a/src/Service/CommentRegistrar.php +++ b/src/Service/CommentRegistrar.php @@ -4,6 +4,7 @@ namespace Aeliot\TodoRegistrar\Service; +use Aeliot\TodoRegistrar\Exception\CommentRegistrationException; use Aeliot\TodoRegistrar\Service\Comment\Detector as CommentDetector; use Aeliot\TodoRegistrar\Service\Comment\Extractor as CommentExtractor; use Aeliot\TodoRegistrar\Service\Registrar\RegistrarInterface; @@ -47,7 +48,11 @@ private function registerTodos(array $tokens): bool if ($this->registrar->isRegistered($todo)) { continue; } - $key = $this->registrar->register($todo); + try { + $key = $this->registrar->register($todo); + } catch (\Throwable $exception) { + throw new CommentRegistrationException($commentPart, $token, $exception); + } $commentPart->injectKey($key); $hasNewTodo = true; } diff --git a/src/Service/TodoFactory.php b/src/Service/TodoFactory.php index 1886eee..3d23136 100644 --- a/src/Service/TodoFactory.php +++ b/src/Service/TodoFactory.php @@ -36,7 +36,7 @@ private function getInlineConfig(string $description): InlineConfigInterface try { $config = $this->inlineConfigReader->getInlineConfig($description); } catch (\Throwable $exception) { - fwrite(STDERR, "[ERROR] Cannot parse inline config: {$exception->getMessage()} for $description \n"); + fwrite(STDERR, "[ERROR] {$exception->getMessage()}. Cannot parse inline config for: $description \n"); $config = []; }