Skip to content

Commit 7132ff5

Browse files
authored
[8.x] Handle ->sole() exceptions (#35912)
* handle sole exceptions * do not handle MultipleRecordsFoundException
1 parent f646f8a commit 7132ff5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Illuminate/Foundation/Exceptions/Handler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ protected function prepareException(Throwable $e)
373373
$e = new HttpException(419, $e->getMessage(), $e);
374374
} elseif ($e instanceof SuspiciousOperationException) {
375375
$e = new NotFoundHttpException('Bad hostname provided.', $e);
376+
} elseif ($e instanceof RecordsNotFoundException) {
377+
$e = new NotFoundHttpException('Not found.', $e);
376378
}
377379

378380
return $e;

tests/Foundation/FoundationExceptionsHandlerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
99
use Illuminate\Contracts\Support\Responsable;
1010
use Illuminate\Contracts\View\Factory;
11+
use Illuminate\Database\RecordsNotFoundException;
1112
use Illuminate\Foundation\Exceptions\Handler;
1213
use Illuminate\Http\RedirectResponse;
1314
use Illuminate\Http\Request;
@@ -235,6 +236,23 @@ public function testSuspiciousOperationReturns404WithoutReporting()
235236

236237
$this->handler->report(new SuspiciousOperationException('Invalid method override "__CONSTRUCT"'));
237238
}
239+
240+
public function testRecordsNotFoundReturns404WithoutReporting()
241+
{
242+
$this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(true);
243+
$this->request->shouldReceive('expectsJson')->once()->andReturn(true);
244+
245+
$response = $this->handler->render($this->request, new RecordsNotFoundException());
246+
247+
$this->assertEquals(404, $response->getStatusCode());
248+
$this->assertStringContainsString('"message": "Not found."', $response->getContent());
249+
250+
$logger = m::mock(LoggerInterface::class);
251+
$this->container->instance(LoggerInterface::class, $logger);
252+
$logger->shouldNotReceive('error');
253+
254+
$this->handler->report(new RecordsNotFoundException());
255+
}
238256
}
239257

240258
class CustomException extends Exception

0 commit comments

Comments
 (0)