2

I'm trying to send errors and exceptions to raygun.io from a Laravel 4 artisan command, but Laravel appears to have it's own exception handler in place.

Is there a way for me to specify a custom method in my Command? Currently, I'm trying to specify a custom handler for errors and exceptions as follows:

<?php
class ParseCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'my:parse';

    protected $descriptino = '...';

    protected $raygun = null;

    /**
     * __construct
     */
    public function __construct()
    {
        parent::__construct();

        // Setup custom error and exception handlers
        $raygun_api_key = \Config::get('tms.raygun_api_key');
        $this->raygun = new RaygunClient("MUzf+furi8E9tffcHAaJVw==");
        set_exception_handler([$this, 'exceptionHandler']);
        set_error_handler([$this, 'errorHandler']);
    }

    /**
     * Custom exception handler.
     *
     * @param $exception
     */
    public function exceptionHandler($exception)
    {
        $this->raygun->SendException($exception);
    }

    /**
     * Custom error handler.
     *
     * @param $errno
     * @param $errstr
     * @param $errfile
     * @param $errline
     */
    public function errorHandler($errno, $errstr, $errfile, $errline)
    {
        $this->raygun->SendError($errno, $errstr, $errfile, $errline);
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function fire()
    {
        $start_time = microtime(true);
        throw new \Exception("Oopsie!");

        // Omitted for brevity...
    }
}

Off course the handlers never execute, as Artisan is grabbing them with it's own custom implementation.

Matteo
  • 37,680
  • 11
  • 100
  • 115
josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157
  • Check in the file `app/start/global.php`. There should be an exception handler there which Laravel would use. It should be pretty easy to modify. I found this http://laravel.com/docs/errors#handling-errors in the docs which should give you more info. – user1669496 Apr 14 '14 at 18:44
  • Related: I wrote [this answer](https://stackoverflow.com/q/46819944/5209322) which may help in newer versions of Laravel. – Cy Rossignol Oct 19 '17 at 00:37

1 Answers1

3

The files in the folder app/start are only executed while booting the Laravel framework when the according environment is run. This means that app/start/local.php is run when the environment is equal to local and app/start/staging.php is run when the environment is equal to staging. Also the app/start/global.php file is run every time and app/start/artisan.php is run on every artisan execution.

From the documentation: http://laravel.com/docs/errors place the handler

App::error(function(Exception $exception)
{
    Log::error($exception);
});

in app/start/artisan for your artisan-only exception handlers.

josef.van.niekerk
  • 11,941
  • 20
  • 97
  • 157
hannesvdvreken
  • 4,858
  • 1
  • 15
  • 17
  • Is your snippet from a previous version on Laravel? Nonetheless, it was very helpful, and I came accross http://laravel.com/docs/errors, didn't even know that was there. Would you mind editing your answer so I can accept it? – josef.van.niekerk Apr 15 '14 at 10:02
  • What do you want to change? Maybe propose an edit yourself? You're welcome. – hannesvdvreken Apr 15 '14 at 10:06