2016-11-14 1 views
1

예외 처리시 오류의 심각도는 어떻게 확인할 수 있습니까? 오류가있을 때마다 전자 메일 알림을 추가했지만 어떤 이유로 인해 유효성 검사 실패시 알림을 받고 있습니다. 에서Laravel 5에서 오류 심각도 가져 오기

public function report(\Exception $e) 
{ 
    if ($e instanceof \Exception) { 
     $top = $e->getMessage().' on line '.$e->getLine(); 
     $body = $e->getTraceAsString(); 

     Mail::queue('emails.general', compact('top','body'), function($message) { 
      $message->from('[email protected]','abc'); 
      $message->to('[email protected]','abc') 
       ->subject('An error on Abc'); 
     }); 

    } 

    parent::report($e); 
} 
+0

어떤 Laravel 버전을 사용하고 있습니까?'app/Exceptions/Handler.php' 코드를 게시 할 수 있습니까? – Jonathon

+0

질문을 수정했습니다. –

+1

작은 노트인데,'if' 문은 항상 적중합니다. '$ e'는'\ Exception'이 아닌 다른 것입니다. – user3158900

답변

3

App\Exceptions\Handler 클래스의 당신의 report 함수에서 당신이 할 수 있습니다 :

if ($this->shouldReport($e)) { 
    // send email 
} 

이 나열된 예외를보고하지 않습니다 $dontReport 변수에 있습니다.

public function report(\Exception $e) 
{ 
    if ($this->shouldReport($e)) { 
     $top = $e->getMessage().' on line '.$e->getLine(); 
     $body = $e->getTraceAsString(); 

     Mail::queue('emails.general', compact('top','body'), function($message) { 
      $message->from('[email protected]','abc'); 
      $message->to('[email protected]','abc') 
       ->subject('An error on Abc'); 
     }); 
    } 

    parent::report($e); 
} 

을 그리고 당신은 예외의 전체 스택 추적을 보내려면 또한 당신이 github REPO를 사용할 수 있습니다

그래서 최종 코드는 볼 것이다.

+0

잘 했어! 두 핸들러 클래스 간의 연결을 자세히 설명 할 수 있습니까? –

+0

이 문서는 docs에서 더 읽을 수 있습니다 .. https://laravel.com/docs/5.3/errors#the-exception-handler –

0

당신의 app/Exceptions/Handler.php 당신이 가보고되지 할 예외 Laravel을 알릴 수있다.

에 추가 할 수있는 $dontReport 속성이 있어야 신고가 중단되어 이메일 전송이 중단됩니다.

<?php 

namespace App\Exceptions; 

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

class Handler extends ExceptionHandler 
{ 
    /** 
    * A list of the exception types that should not be reported. 
    * 
    * @var array 
    */ 
    protected $dontReport = [ 
     AuthorizationException::class, 
     HttpException::class, 
     ModelNotFoundException::class, 
     ValidationException::class, 
    ]; 

    /** 
    * Report or log an exception. 
    * 
    * This is a great spot to send exceptions to Sentry, Bugsnag, etc. 
    * 
    * @param \Exception $e 
    * @return void 
    */ 
    public function report(Exception $e) 
    { 
     parent::report($e); 
    } 

    /** 
    * Render an exception into an HTTP response. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Exception $e 
    * @return \Illuminate\Http\Response 
    */ 
    public function render($request, Exception $e) 
    { 
     return parent::render($request, $e); 
    } 
} 

이 내 Handler.php 파일입니다. ValidationException::class$dontReport 속성에 있으며보고되지 않는다는 것을 알 수 있습니다.

다른 방법으로, 특히 단지 ValidationException를 무시하도록 코드를 수정할 수 :

if ($e instanceof \Exception && !($e instanceof ValidationException)) { 
    // Your email code... 
+0

거기에 $ dontReport 변수가 있고 ValidationException :: class도 있습니다. ValidationException은 로그에 저장되지 않지만 그럼에도 불구하고 그것에 대한 전자 메일을 받고 있습니다. 내 메일 링 함수 내에서 $ dontReport를 사용하여 $ dontReport에 나타나는 모든 예외를 무시할 수있는 방법이 있습니까? –

+0

그들은 그때 아직도보고되고 있다는 것이 이상합니다. 그래, 그렇게 할 수있는 방법이 있어야 해. 내 대답을 업데이트 할게. 어떤 Laravel 버전을 사용하고 있습니까? – Jonathon

+0

오, 걱정 마세요. 이미 질문에 대한 답변을 얻은 것으로 보입니다. – Jonathon