Skip to content

Commit

Permalink
[8.x] Handle ->sole() exceptions (#35912)
Browse files Browse the repository at this point in the history
* handle sole exceptions

* do not handle MultipleRecordsFoundException
  • Loading branch information
rodrigopedra committed Jan 15, 2021
1 parent f646f8a commit 7132ff5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Expand Up @@ -373,6 +373,8 @@ protected function prepareException(Throwable $e)
$e = new HttpException(419, $e->getMessage(), $e);
} elseif ($e instanceof SuspiciousOperationException) {
$e = new NotFoundHttpException('Bad hostname provided.', $e);
} elseif ($e instanceof RecordsNotFoundException) {
$e = new NotFoundHttpException('Not found.', $e);
}

return $e;
Expand Down
18 changes: 18 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\View\Factory;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -235,6 +236,23 @@ public function testSuspiciousOperationReturns404WithoutReporting()

$this->handler->report(new SuspiciousOperationException('Invalid method override "__CONSTRUCT"'));
}

public function testRecordsNotFoundReturns404WithoutReporting()
{
$this->config->shouldReceive('get')->with('app.debug', null)->once()->andReturn(true);
$this->request->shouldReceive('expectsJson')->once()->andReturn(true);

$response = $this->handler->render($this->request, new RecordsNotFoundException());

$this->assertEquals(404, $response->getStatusCode());
$this->assertStringContainsString('"message": "Not found."', $response->getContent());

$logger = m::mock(LoggerInterface::class);
$this->container->instance(LoggerInterface::class, $logger);
$logger->shouldNotReceive('error');

$this->handler->report(new RecordsNotFoundException());
}
}

class CustomException extends Exception
Expand Down

0 comments on commit 7132ff5

Please sign in to comment.