Skip to content
Advertisement

How can I display Exception trace in laravel console command?

I’m using Laravel 5.1 to create a console based application. During development I would like to display the exception trace when an error occurs. However, even if I use -v -vv or -vvv option in php artisan, I don’t get an exception trace for my custom commands. I set APP_DEBUG=true in my .env, still no exception trace.

Output of php artisan some:unknowncommand is:

[InvalidArgumentException]                              
There are no commands defined in the "some" namespace.

Output of php artisan -v some:unknowncommand is:

[InvalidArgumentException]                              
  There are no commands defined in the "some" namespace.  

Exception trace:
 () at /Users/dirkpostma/Dropbox/Domains/dpepp/vendor/symfony/console/Application.php:501
 SymfonyComponentConsoleApplication->findNamespace() at /Users/dirkpostma/Dropbox/Domains/dpepp/vendor/symfony/console/Application.php:535
 SymfonyComponentConsoleApplication->find() at /Users/dirkpostma/Dropbox/Domains/dpepp/vendor/symfony/console/Application.php:192
 SymfonyComponentConsoleApplication->doRun() at /Users/dirkpostma/Dropbox/Domains/dpepp/vendor/symfony/console/Application.php:126
...

Now, I created a very simple console command called dp:test, with following handle function:

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    generate error here
}

Output of php artisan dp:test is:

[SymfonyComponentDebugExceptionFatalErrorException]
syntax error, unexpected 'error' (T_STRING)    

Output of php artisan -v dp:test is the same. Output of php artisan -vvv dp:test is the same.

The log file DOES show the exception trace, so somehow it should be possible to display it in cli. I don’t even see the filename and linenumer where the error occurs… How can I take care of this?

Thanks in advance!

EDIT:

Is digged a bit further. In case I use this in my Command:

public function handle()
{
    throw new Exception("test exception");
}

and I issue the command php artisan -v dp:test, the error trace IS printed. The trace is only not printed when the exception is thrown due to a PHP error. In Illuminate/Foundation/Bootstrap/HandleExceptions.php method bootstrap PHP errors are converted to Exceptions. When this occurs, an exception is thrown, but the -v is somehow ignored when printing. This is very inconvenient because it makes debugging CLI apps hard.

I think the solution can be found in vendor/symfony/console/Application.php, method renderException.

I’m going to dig further later, unless someone else can point the solution faster than me 🙂

Advertisement

Answer

I found reason why -v is ignored:

in Illuminate/Foundation/Bootstrap/HandleExceptions.php, the renderForConsole method instantiates a ConsoleOutput object, with default settings, not taking into account the verbosity settings the user asked for:

protected function renderForConsole($e)
{
    $this->getExceptionHandler()->renderForConsole(new ConsoleOutput, $e);
}

For this reason, whatever -v -vv or -vvv is set, the $output->getVerbosity() in vendor/symfony/console/Application.php is always lower than OutputInterface::VERBOSITY_VERBOSE, for that reason, the stack trace is not printed.

I probably start an issue on github for this, because I think it’s much more convenient if errors are shown in CLI if user sets -v.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement