LoginSignup
16
18

More than 3 years have passed since last update.

【Laravel】例外処理(try...catch)でのデバッグ

Last updated at Posted at 2020-06-21

はじめに

例外を発生させたい時、try...catch文を使うかと思います。
その時のデバッグ方法についてメモです。

これまで

test.php
try {
// 略 
} catch (\Exception $e) {
    $e->getMessage();
    session()->flash('flash_message', '更新が失敗しました');
}

というような感じでgetMessageメソッドで使っておりました。
これだとエラーの原因(ex:undefined $hoge)は分かるのですが、どの行でエラーを起こしているかという情報までは出力されないんですよね。。。

というわけで、、、

こうしました

test.php
try {
// 略 
} catch (\Exception $e) {
    report($e);
    session()->flash('flash_message', '更新が失敗しました');
}

reportメソッドを使うと、どのファイルの何行目で処理が止まっているか(エラーが起きているか)まで出力してくれます。

16
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
18