2016-04-15 79 views
1

我有一个Laravel应用程序,它将例外发送到Errbit服务器。防止Laravel Artisan例外发送到错误报告

下面是异常处理程序的代码:

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

虽然这个作品真的很好总的来说,我想避免当我运行工匠命令中发生错误记录。记录错误的全部要点是要通知一个问题,但是如果我坐在控制台上,我已经知道这个问题。

我想寻找的堆栈跟踪,看是否工匠存在的,但我看到两个问题是:

  • 这听起来不像一个非常有效的事情在做。
  • 队列侦听器也是通过工匠运行的,而且我确实需要从这些工程中获得例外,因为它们实际上并未从控制台运行。

如何跳过从控制台运行的所有异常的异常报告,但保留所有其他异常的异常报告?

回答

0

实际运行artisan命令的Illuminate\Foundation\Console\Kernel类有一个函数reportException它调用你的ExceptionHandlerreport方法。

我补充说,方法的重写我的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); 
}