Moshe Katz
Moshe Katz

Reputation: 16883

Prevent Laravel Artisan exceptions from being sent to error reporting

I have a Laravel app that sends its exceptions to an Errbit server.

Here is the code for the exception handler:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Validation\ValidationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

use Airbrake\Notifier;

class Handler extends ExceptionHandler
{
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
        TokenMismatchException::class,
    ];

    public function report(Exception $e)
    {
        if ($this->shouldReport($e)) {
            $options = [
                'environment'     => app()->environment(),
                'host'            => config('airbrake.server'),
                'projectId'       => config('airbrake.api_key'),
                'projectKey'      => config('airbrake.api_key'),
                'rootDirectory'   => base_path(),
                'secure'          => TRUE,
                'url'             => request()->fullUrl(),
            ];

            $notifier = new Notifier($options);
            $notifier->notify($e);
        }

        return parent::report($e);
    }

    public function render($request, Exception $e)
    {
        // Replace `ModelNotFound` with 404.
        if ($e instanceof ModelNotFoundException) {
            $message = 'Page not found';
            if (config('app.debug')) {
                $message = $e->getMessage();
            }
            $e = new NotFoundHttpException($message, $e);
        }

        $response = parent::render($request, $e);

        return $response;
    }
}

While this works really well in general, I want to avoid logging errors that happen when I run artisan commands. The whole point of logging errors is to be notified of a problem, but if I'm sitting at the console, I already know about the problem.

I thought of looking in the stack trace to see if artisan is present there, but I see two problems with that:

How can I skip exception reporting for all exceptions run from the console, but keep it on for all the others?

Upvotes: 1

Views: 1201

Answers (1)

Moshe Katz
Moshe Katz

Reputation: 16883

The Illuminate\Foundation\Console\Kernel class that actually runs artisan commands has a function reportException which calls your ExceptionHandler's report method.

I added an override of that method to my Kernel class that checks if STDIN is an interactive terminal and disables the error reporting:

protected function reportException(Exception $e)
{
    // Disable exception reporting if run from the console.
    if (function_exists('posix_isatty') && @posix_isatty(STDIN)) {
        echo "Not sending exception report";
        return;
    }

    parent::reportException($e);
}

Upvotes: 2

Related Questions