Skip to content

Commit 2a87f91

Browse files
Declare exceptions unreportable using the ShouldntReport interface (#52337)
* Added ability to define exceptions as unreportable using ShouldntReport interface * Update ShouldntReport.php * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7dc8c26 commit 2a87f91

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Contracts\Debug;
4+
5+
interface ShouldntReport
6+
{
7+
//
8+
}

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Console\View\Components\Error;
1414
use Illuminate\Contracts\Container\Container;
1515
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
16+
use Illuminate\Contracts\Debug\ShouldntReport;
1617
use Illuminate\Contracts\Foundation\ExceptionRenderer;
1718
use Illuminate\Contracts\Support\Responsable;
1819
use Illuminate\Database\Eloquent\ModelNotFoundException;
@@ -401,6 +402,10 @@ protected function shouldntReport(Throwable $e)
401402
return true;
402403
}
403404

405+
if ($e instanceof ShouldntReport) {
406+
return true;
407+
}
408+
404409
$dontReport = array_merge($this->dontReport, $this->internalDontReport);
405410

406411
if (! is_null(Arr::first($dontReport, fn ($type) => $e instanceof $type))) {

tests/Integration/Foundation/ExceptionHandlerTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Auth\Access\AuthorizationException;
66
use Illuminate\Auth\Access\Response;
77
use Illuminate\Contracts\Debug\ExceptionHandler;
8+
use Illuminate\Contracts\Debug\ShouldntReport;
9+
use Illuminate\Contracts\Support\Responsable;
810
use Illuminate\Support\Facades\Config;
911
use Illuminate\Support\Facades\Route;
1012
use Orchestra\Testbench\TestCase;
@@ -42,6 +44,30 @@ public function testItRendersAuthorizationExceptions()
4244
]);
4345
}
4446

47+
public function testItDoesntReportExceptionsWithShouldntReportInterface()
48+
{
49+
Config::set('app.debug', true);
50+
$reported = [];
51+
$this->app[ExceptionHandler::class]->reportable(function (Throwable $e) use (&$reported) {
52+
$reported[] = $e;
53+
});
54+
55+
$exception = new class extends \Exception implements ShouldntReport, Responsable {
56+
public function toResponse($request)
57+
{
58+
return response('shouldnt report', 500);
59+
}
60+
};
61+
62+
Route::get('test-route', fn () => throw $exception);
63+
64+
$this->getJson('test-route')
65+
->assertStatus(500)
66+
->assertSee('shouldnt report');
67+
68+
$this->assertEquals([], $reported);
69+
}
70+
4571
public function testItRendersAuthorizationExceptionsWithCustomStatusCode()
4672
{
4773
Route::get('test-route', fn () => Response::deny('expected message', 321)->withStatus(404)->authorize());

0 commit comments

Comments
 (0)