LoginSignup
7
3

More than 5 years have passed since last update.

Laravel 5.3 『Whoops, looks like something went wrong.』 をやめよう!

Last updated at Posted at 2016-12-27

Whoops, looks like something went wrong.

  • Whoops…
  • ウープス…
  • うーぷす…

.env の APP_DEBUG=false としても何かしらのエラーのタイトルの一文のみが表示されてしまう!
そもそも真っ白もありえないけど…

はい!できるだけ何が起こってるか画面で伝えよう!

下のdiffは自分が気になった所、3点を改修!
【app\Exceptions\Handler.php】をそれぞれ上から

  • トークン エラー
  • Syntax エラー バリデーション エラー
  • http ステータスコード
Handler.php
+ use Illuminate\Session\TokenMismatchException;
+ use Illuminate\Validation\ValidationException;

    public function render($request, Exception $exception)
    {
+         if ($exception instanceof TokenMismatchException) {
+             return redirect()->back()->with('message','セッションのタイムアウトとなりました。もう一度入力してください。');
+         }
+         if (get_class($exception) == "ErrorException" || $exception instanceof ValidationException) {
+             return parent::render($request, $exception);
+         }
+         $status = $exception->getStatusCode();
+         return response()->view("errors.common", ['exception' => $exception], $status, $exception->getHeaders());
-         return parent::render($request, $exception);
    }

解説

use hoge\hoge;

例外を判断するときに必要な呪文みたいなもの!(あんまり理解してない)

if ($exception instanceof TokenMismatchException) {

トークンエラーの際に、messageをセットして戻す。(oldとか改善の余地あり)

if (get_class($exception) == "ErrorException" || $exception instanceof ValidationException) {

Syntaxエラー → クラスで無理やり表示("ErrorException")
それぞれ『parent::render』で満足いく結果になった。

$status = $exception->getStatusCode(); 以下

httpステータスコードによりVIEW側で表示の出し分けをする。

さいごに

全く解説になってないけど、この辺りのドキュメントがあまりヒットしなかったら書いてみた!
そして、もっとスマートなやり方もあるんだろうなーっと!
気が向いたら編集しよう!

7
3
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
7
3