Prevent Laravel Artisan exceptions from being sent to error reporting

时间:2016-04-15 14:56:27

标签: laravel exception-handling laravel-5.2

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:

  • It doesn't sound like a very efficient thing to be doing.
  • Queue listeners are also run through artisan, and I do need to get the exceptions from those, because they are not actually being run from the console.

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

1 个答案:

答案 0 :(得分:0)

实际运行Illuminate\Foundation\Console\Kernel命令的artisan类具有调用reportException ExceptionHandler方法的函数report

我在我的Kernel类中添加了该方法的覆盖,检查STDIN是否为交互式终端并禁用错误报告:

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);
}