KnpLabs / ConsoleServiceProvider

A Console service provider for Silex
MIT License
107 stars 37 forks source link

Handling exeptions #28

Closed lukaszwit closed 7 years ago

lukaszwit commented 8 years ago

Hi, is there any way to handle "ConsoleExceptionEvent" similar to Symfony commands: http://symfony.com/doc/current/console/logging.html ?

luiz-gustavo-agostinho commented 7 years ago

I'm also interested in this.

Nek- commented 7 years ago

You may consider a pull request but this project is not actively maintained. Sorry !

luiz-gustavo-agostinho commented 7 years ago

Thank you for the reply. For the record I manage to handle the exceptions by extending \Knp\Command\Command and overwrite the run method, adding a try catch, in this way I can log all command executions that somehow failed.

class Command extends \Knp\Command\Command
{
    public function run(InputInterface $input, OutputInterface $output)
    {
        try {
            return parent::run($input, $output);
        } catch (\Exception $e) {
            $this->getSilexApplication()->getLogger()->error($e);
        }
    }
skalpa commented 7 years ago

This will be possible once #31 gets merged (the example I added to the documentation actually shows how to listen to the ConsoleExceptionEvent and log the error).

In the meantime, something like what @agostlg proposed should also work.

skalpa commented 7 years ago

Fixed by #31. You can now register listeners for Symfony\Component\Console\ConsoleEvents events.