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

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

merged 2 commits into from
Jan 15, 2021

Conversation

rodrigopedra
Copy link
Contributor

@rodrigopedra rodrigopedra commented Jan 15, 2021

This PR is a follow up of PRs #35902 and #35908

In addition on not reporting the exceptions thrown by calling ->sole() (which is handled by PR #35908) this PR adds two elseif clauses to \Illuminate\Foundation\Exceptions@prepareException to handle both RecordsNotFoundException (when not called from an Eloquent\Builder) and MultipleRecordsFoundException.

Tests were added for both exceptions handling.


EDIT: After conversation with @GrahamCampbell , I just kept the RecordsNotFoundException handling. Leaving the MultipleRecordsFoundException to be handled as a regular exception which defaults to a 500.

@@ -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

@rodrigopedra rodrigopedra Jan 15, 2021

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants