2016-04-15 80 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); 
}