Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use ErrorListiner without logger + Minor refactoring #194

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 2.1.3 under development

- no changes in this release.
- Enh #194: Allow to use `ErrorListiner` without logger (@vjik)

## 2.1.2 December 26, 2023

Expand All @@ -14,7 +14,7 @@

## 2.1.0 May 28, 2023

- Bug #172: Fix accepting `:` as command name separator, offer using it by default (samdark)
- Bug #172: Fix accepting `:` as command name separator, offer using it by default (@samdark)
- Bug #179: Remove duplicate messages about server address (@samdark)
- Enh #180: Enhance output of `serve` command, add `--xdebug` option for `serve` (@xepozz)

Expand Down
13 changes: 6 additions & 7 deletions src/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,26 @@

final class ErrorListener
{
public function __construct(private LoggerInterface $logger)
public function __construct(private ?LoggerInterface $logger = null)
{
}

/**
* @psalm-suppress PossiblyNullArgument
*/
public function onError(ConsoleErrorEvent $event): void
{
if ($this->logger === null) {
return;

Check warning on line 21 in src/ErrorListener.php

View check run for this annotation

Codecov / codecov/patch

src/ErrorListener.php#L21

Added line #L21 was not covered by tests
}

$exception = $event->getError();
$command = $event->getCommand();

$commandName = ($command !== null && $command->getName() !== null) ? $command->getName() : 'unknown';

$message = sprintf(
'%s: %s in %s:%s while running console command "%s".',
$exception::class,
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
$commandName,
$command?->getName() ?? 'unknown',
);

$this->logger->error($message, ['exception' => $exception]);
Expand Down
Loading