Skip to content

Commit bc76c95

Browse files
Improves command not found (#43323)
Co-Authored-By: Slava Razum <[email protected]> Co-authored-by: Slava Razum <[email protected]>
1 parent 8a0db35 commit bc76c95

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Exception;
77
use Illuminate\Auth\Access\AuthorizationException;
88
use Illuminate\Auth\AuthenticationException;
9+
use Illuminate\Console\View\Components\BulletList;
10+
use Illuminate\Console\View\Components\Error;
911
use Illuminate\Contracts\Container\Container;
1012
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
1113
use Illuminate\Contracts\Foundation\ExceptionRenderer;
@@ -30,6 +32,7 @@
3032
use Psr\Log\LoggerInterface;
3133
use Psr\Log\LogLevel;
3234
use Symfony\Component\Console\Application as ConsoleApplication;
35+
use Symfony\Component\Console\Exception\CommandNotFoundException;
3336
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
3437
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
3538
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;
@@ -718,6 +721,23 @@ protected function convertExceptionToArray(Throwable $e)
718721
*/
719722
public function renderForConsole($output, Throwable $e)
720723
{
724+
if ($e instanceof CommandNotFoundException) {
725+
$message = str($e->getMessage())->explode('.')->first();
726+
727+
if (! empty($alternatives = $e->getAlternatives())) {
728+
$message .= '. Did you mean one of these?';
729+
730+
with(new Error($output))->render($message);
731+
with(new BulletList($output))->render($e->getAlternatives());
732+
733+
$output->writeln('');
734+
} else {
735+
with(new Error($output))->render($message);
736+
}
737+
738+
return;
739+
}
740+
721741
(new ConsoleApplication)->renderThrowable($e, $output);
722742
}
723743

0 commit comments

Comments
 (0)