-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Conversation
@@ -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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
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 twoelseif
clauses to\Illuminate\Foundation\Exceptions@prepareException
to handle bothRecordsNotFoundException
(when not called from anEloquent\Builder
) andMultipleRecordsFoundException
.Tests were added for both exceptions handling.
EDIT: After conversation with @GrahamCampbell , I just kept the
RecordsNotFoundException
handling. Leaving theMultipleRecordsFoundException
to be handled as a regular exception which defaults to a 500.