Skip to content

Commit

Permalink
Make error of inline config parsing more informative
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeliot-Tm committed Jul 31, 2024
1 parent 43911e8 commit 6678b40
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Aeliot\TodoRegistrar;

use Aeliot\TodoRegistrar\Exception\CommentRegistrationException;
use Aeliot\TodoRegistrar\Service\File\Finder;
use Aeliot\TodoRegistrar\Service\FileProcessor;

Expand All @@ -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);
}
}
28 changes: 28 additions & 0 deletions src/Exception/CommentRegistrationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Aeliot\TodoRegistrar\Exception;

use Aeliot\TodoRegistrar\Dto\Comment\CommentPart;

final class CommentRegistrationException extends \RuntimeException
{
public function __construct(
private CommentPart $commentPart,
private \PhpToken $token,
\Throwable $previous,
) {
parent::__construct(sprintf('Cannot register %s-comment', $this->commentPart->getTag()), 0, $previous);
}

public function getCommentPart(): CommentPart
{
return $this->commentPart;
}

public function getToken(): \PhpToken
{
return $this->token;
}
}
7 changes: 6 additions & 1 deletion src/Service/CommentRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Service/TodoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}

Expand Down

0 comments on commit 6678b40

Please sign in to comment.