[11.x] Allow easier overriding of the exception thrown by invalid ID in route binding#53944
Merged
taylorotwell merged 3 commits intoDec 17, 2024
Conversation
shaedrich
reviewed
Dec 17, 2024
| */ | ||
| protected function handleInvalidUniqueId($value, $field) | ||
| { | ||
| throw (new ModelNotFoundException)->setModel(get_class($this), $value); |
Contributor
There was a problem hiding this comment.
You could even put the exception name as static property on the model so ModelNotFoundException is taken as a default but can be overwritten without overwriting handleInvalidUniqueId() when one just wants to change the class name
Suggested change
| throw (new ModelNotFoundException)->setModel(get_class($this), $value); | |
| throw (new $notFoundException)->setModel(get_class($this), $value); |
Contributor
Author
There was a problem hiding this comment.
That's a good idea, but I think it would require the class to implement setModel() which isn't enforced by an interface.
Contributor
There was a problem hiding this comment.
Good point 👍🏻 Maybe this can be implemented via an interface then 🤔
trait HasUniqueStringIds
{
+ private ModelException $notFoundException = ModelNotFoundExceptioninterface ModelException
{
/**
* Set the affected Eloquent model and instance ids.
*
* @param class-string<TModel> $model
* @param array<int, int|string>|int|string $ids
* @return $this
*/
public function setModel($model, $ids = []);
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is to make it easier to handle invalid route keys without having to override
HasUniqueStringIds@resolveRouteBindingQuery()method (which would be a lot of duplication and create maintenance overhead).This consolidates code a little bit into a common method
handleInvalidUniqueIdwhich should throw an exception. There is no breaking change here, as we're still throwing the sameModelNotFoundException. For a team that wishes to customize their return type (such as indicating this is a 422 response, not a 404), they can easily do that as follows: