Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Handle ->sole() exceptions #35912

Merged
merged 2 commits into from Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Expand Up @@ -373,6 +373,10 @@ 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);
} elseif ($e instanceof MultipleRecordsFoundException) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might also mean internal server error due to the database getting into a bad state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could think of that.

The use case I thought was filter constraints being passed and only one result being expected/allowed to an endpoint. Which would be a user error, not a server error.

I guess the RecordsNotFoundException is fine as it is, but maybe we could change the MultipleRecordsFoundException to be a 500 instead.

I can update the PR to whichever you find better. The goal is to have it handled gracefully as a request exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB Changing to 500 amounts to just deleting this line and the line below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And removing the test for it. Just committed, thanks!

$e = new HttpException(400, 'Bad request.', $e);
}

return $e;
Expand Down
36 changes: 36 additions & 0 deletions tests/Foundation/FoundationExceptionsHandlerTest.php
Expand Up @@ -8,6 +8,8 @@
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\View\Factory;
use Illuminate\Database\MultipleRecordsFoundException;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -235,6 +237,40 @@ 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());
}

public function testMultipleRecordsFoundReturns400WithoutReporting()
{
$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 MultipleRecordsFoundException());

$this->assertEquals(400, $response->getStatusCode());
$this->assertStringContainsString('"message": "Bad request."', $response->getContent());

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

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

class CustomException extends Exception
Expand Down