0

Laravel 5.3でカスタム例外をいくつか実装しようとしています。Laravelカスタム例外設定

私はExceptionクラスを拡張するAppExceptionを作成しました。これの考え方は、私が例外にユーザーを付けるので、誰がそれをトリップしてログに記録できるのかを知りたいということです。

だから私はこのように私のカスタム例外設定:

class AppException extends Exception{ 

    protected $userId; 

    public function __construct($message = '', $userId=0) 
    { 
     parent::__construct($message); 
     $this->userId = $userId; 
    } 
} 

は、今私が望むものを私のコントローラの機能で試しキャッチのを使用して、例外をキャッチして、私は接続することができ、アプリケーションの例外を再スローすることですユーザー。このように:

try{ 
    << code here >> 
} 
catch(Exception $e){ 
    throw new AppException($e->getMessage, $request->user()->id); 
} 

私が発見しています私は私の例外からログインしています行ではない実際の例外から、キャッチで再スローからのラインですので、私は良いトレーススタックを取得することができませんということです。

これを設定する正しい方法は何でしょうか?私は、すべてのtry catchでログコードを入力する代わりに、Laravelに付属の組み込みのHandler.phpファイルを利用できる方法でこれを実行しようとしています。

ありがとうございます!

答えて

0

を参照してください。

class AppException extends Exception 
{ 
    private $userId; 

    public function __construct($userId, $message = '', $code = 0, Exception $previous = null) 
    { 
     $this->userId = $userId; 

     parent::__construct($message, $code, $previous); 
    } 

    public static function fromUserId($userId, Exception $previous = null) 
    { 
     return new static($userId, sprintf('Exception thrown from `%s` userid', $userId), 0, $previous); 
    } 

    public function getUserId() 
    { 
     return $this->userId; 
    } 
} 

それとも単に

class AppException extends Exception 
{ 
    public static function fromUserId($userId, Exception $previous = null) 
    { 
     return new static(sprintf('Exception thrown from `%s` userid', $userId), 0, $previous); 
    } 
} 

あなたがAppExceptionをキャッチした後、あなたは、このようにすべての例外を反復することができます

do { 
    printf("%s:%d %s (%d) [%s]\n", $e->getFile(), $e->getLine(), $e->getMessage(), $e->getCode(), get_class($e)); 
} while($e = $e->getPrevious()); 

例:

try { 
    try{ 
     throw new RuntimeException('Something bad happened.'); 
    } 
    catch(Exception $e){ 
     throw AppException::fromUserId(123, $e); 
    } 
} catch(AppException $e) { 
    do { 
     printf("%s:%d %s (%d) [%s]\n", $e->getFile(), $e->getLine(), $e->getMessage(), $e->getCode(), get_class($e)); 
    } while($e = $e->getPrevious()); 
} 
0

は、このように、たとえば、スローされた例外に実際の例外を装着してみてください。

class AppException extends \Exception 
{ 
    /** 
    * @var int 
    */ 
    private $userId; 

    /** 
    * @param \Exception $exception 
    * @param int  $userId 
    * 
    * @return self 
    */ 
    public static function fromExceptionAndUserId(\Exception $exception, $userId) 
    { 
     $instance = new self(
      $exception->getMessage(), 
      0, 
      $exception 
     ); 

     $instance->userId = $userId; 

     return $instance; 
    } 

    /** 
    * @return null|int 
    */ 
    public function userId() 
    { 
     return $this->userId; 
    } 
} 

を次に、あなたのコントローラで:

try { 
    // ... 
} catch (\Exception $exception) { 
    throw AppException::fromExceptionAndUserId(
     $exception, 
     $request->user()->id 
    ); 
} 

アイデアだけではなく、実際の例外メッセージを使用することで、実際の例外を$previous例外として付加することもできます。

名前付きコンストラクタを使用すると、AppExceptionの例外メッセージがどのように(ユーザ識別子も添付して)構成され、元のAppExceptionコンストラクタを使用できるように制御できるという利点があります。さまざまなユースケースに対して、より多くの名前付きコンストラクタを自由に追加することができます。参考のため

Exceptionクラスを使用すると、以前のすべての例外を取得するために使用することができますprevious引数を持ってい