@Jean_ali If you mean the test, here you go:
<?php
declare(strict_types=1);
namespace App\Exceptions;
(…)
class Handler extends ExceptionHandler
{
(…)
/**
* A list of the internal exception types that should not be reported.
* Overridden to remove TokenMismatchException.
*
* @var string[]
*/
protected $internalDontReport = [
AuthenticationException::class,
AuthorizationException::class,
HttpException::class,
HttpResponseException::class,
ModelNotFoundException::class,
MultipleRecordsFoundException::class,
RecordsNotFoundException::class,
SuspiciousOperationException::class,
ValidationException::class,
];
/**
* Determine if the exception should be reported.
* Overridden solely to change the method's visibility.
*/
public function shouldntReport(Throwable $e): bool
{
return parent::shouldntReport($e);
}
}
<?php
declare(strict_types=1);
namespace Tests\Unit;
use App\Exceptions\Handler;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\MultipleRecordsFoundException;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Validation\ValidationException;
use stdClass;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;
use Throwable;
class ExceptionHandlerTest extends TestCase
{
protected array $internalDontReport = [
AuthenticationException::class,
AuthorizationException::class,
HttpException::class,
HttpResponseException::class,
ModelNotFoundException::class,
MultipleRecordsFoundException::class,
RecordsNotFoundException::class,
SuspiciousOperationException::class,
ValidationException::class,
];
protected array $shouldReport = [
TokenMismatchException::class,
];
protected Handler $handler;
protected function setUp(): void
{
parent::setUp();
$this->handler = new Handler($this->createMock(Container::class));
}
protected function newThrowable(string $type): Throwable
{
switch ($type) {
case HttpException::class:
$e = new $type(500);
break;
case HttpResponseException::class:
$e = new $type(new Response());
break;
case ValidationException::class:
$e = new $type(new stdClass());
break;
default:
$e = new $type();
}
return $e;
}
/**
* @test
*/
public function selectExceptionsShouldNotBeReported(): void
{
collect($this->internalDontReport)->each(function (string $type, int $key): void {
$this->assertTrue($this->handler->shouldntReport($this->newThrowable($type)));
});
}
/**
* @test
*/
public function selectExceptionsShouldBeReported(): void
{
collect($this->shouldReport)->each(function (string $type, int $key): void {
$this->assertTrue($this->handler->shouldReport($this->newThrowable($type)));
});
}
}
That test fails if you don't import the exception symbols in App\Exceptions\Handler, but as soon as you do it goes right back to green and, at least in my case, HTTP 4xx exceptions stopped being reported in the error log.