Skip to content

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.

$e = new NotFoundHttpException('Bad hostname provided.', $e);
} elseif ($e instanceof RecordsNotFoundException) {
$e = new NotFoundHttpException('Not found.', $e);
} elseif ($e instanceof MultipleRecordsFoundException) {
Copy link
Collaborator

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
Collaborator

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