- 1. 概要
- 2. 説明
- 3. 例外をキャッチ
- 4. 参考サイト
1. 概要
本ページでは、例外のキャッチについて記述します。
2. 説明
「Laravel」で、存在しない「.blade.php」を表示しようとすると。
[2026-01-19 15:22:29] local.ERROR: View [linux.10.20.05.index] not found. {"exception":"[object] (InvalidArgumentException(code: 0): View [linux.10.20.05.index] not found. at /usr/local/www/laravel/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php:137)
[stacktrace]
#0 /usr/local/www/laravel/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php(79): Illuminate\\View\\FileViewFinder->findInPaths()
#1 /usr/local/www/laravel/vendor/laravel/framework/src/Illuminate/View/Factory.php(137): Illuminate\\View\\FileViewFinder->find()
#2 /usr/local/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(1020): Illuminate\\View\\Factory->make()
・・・ 略 ・・・
#44 /usr/local/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(175): Illuminate\\Pipeline\\Pipeline->then()
#45 /usr/local/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(144): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#46 /usr/local/www/laravel/public/index.php(51): Illuminate\\Foundation\\Http\\Kernel->handle()
#47 {main}
"}
てなログを出力します。
この場合、原因はわかっているわけで、長ったらしいログがでるのも嫌だし、あたかもエラーが発生しているかのようなログになっているのも嫌です。
3. 例外をキャッチ
app/Exceptions/Handler.php
を編集します。
「Laravel Framework 10.48.29」では、デフォルトで下記のように記述されています。
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
//
});
}
}
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
の箇所を下記のように変更します。
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use Throwable;
class Handler extends ExceptionHandler
{
public function render($request, Throwable $e)
{
// Blade が存在しない場合
if ($e instanceof InvalidArgumentException && str_contains($e->getMessage(), 'View ['))
{
Log::error($e->getMessage());
return response()->view('errors.404', [], 404);
}
return parent::render($request, $e);
}
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
InvalidArgumentException::class,
];
下記の箇所で、「View [...] not found.」の箇所のみログへ出力するようにしています。
Log::error($e->getMessage());
下記の箇所が、だらだら出力する例外のログを抑制しています。
protected $dontReport = [
InvalidArgumentException::class,
];
4. 参考サイト
本ページは、「ChatGPT」軍曹を参考にさせていただきました。
|