-
Notifications
You must be signed in to change notification settings - Fork 11.3k
[12.x] Return unprocessable response when parameter is not a valid UniqueStringId #54033
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
Changes from all commits
bf894ad
1cef2ac
3ea649a
74dae59
347cb5a
d02b551
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||||||||||||||
<?php | ||||||||||||||||||
|
||||||||||||||||||
namespace Illuminate\Database\Eloquent; | ||||||||||||||||||
|
||||||||||||||||||
use Illuminate\Support\Arr; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* @template TModel of \Illuminate\Database\Eloquent\Model | ||||||||||||||||||
*/ | ||||||||||||||||||
class InvalidIdFormatException extends \RuntimeException | ||||||||||||||||||
{ | ||||||||||||||||||
/** | ||||||||||||||||||
* Name of the affected Eloquent model. | ||||||||||||||||||
* | ||||||||||||||||||
* @var class-string<TModel> | ||||||||||||||||||
*/ | ||||||||||||||||||
protected $model; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* The affected model IDs. | ||||||||||||||||||
* | ||||||||||||||||||
* @var array<int, int|string> | ||||||||||||||||||
*/ | ||||||||||||||||||
protected $ids; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Set the affected Eloquent model and instance id. | ||||||||||||||||||
* | ||||||||||||||||||
* @param class-string<TModel> $model | ||||||||||||||||||
* @param array<int, int|string>|int|string $ids | ||||||||||||||||||
* @param string|null $field | ||||||||||||||||||
* @return $this | ||||||||||||||||||
*/ | ||||||||||||||||||
public function setModel($model, $ids = [], ?string $field = null) | ||||||||||||||||||
{ | ||||||||||||||||||
$this->model = $model; | ||||||||||||||||||
$this->ids = Arr::wrap($ids); | ||||||||||||||||||
Comment on lines
+34
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you type-hint
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
$this->message = 'Invalid key'; | ||||||||||||||||||
if ($field !== null) { | ||||||||||||||||||
$this->message .= " [{$field}]"; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
$this->message .= " for model [{$model}]"; | ||||||||||||||||||
|
||||||||||||||||||
if (count($this->ids) > 0) { | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified by omitting the function call and comparing it to an empty scalar value:
Suggested change
|
||||||||||||||||||
$this->message .= ' '.implode(', ', $this->ids); | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
$this->message .= '.'; | ||||||||||||||||||
|
||||||||||||||||||
return $this; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Get the affected Eloquent model. | ||||||||||||||||||
* | ||||||||||||||||||
* @return class-string<TModel> | ||||||||||||||||||
*/ | ||||||||||||||||||
public function getModel() | ||||||||||||||||||
{ | ||||||||||||||||||
return $this->model; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* Get the affected Eloquent model IDs. | ||||||||||||||||||
* | ||||||||||||||||||
* @return array<int, int|string> | ||||||||||||||||||
*/ | ||||||||||||||||||
public function getIds() | ||||||||||||||||||
{ | ||||||||||||||||||
return $this->ids; | ||||||||||||||||||
} | ||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ | |||||
use Illuminate\Contracts\Debug\ShouldntReport; | ||||||
use Illuminate\Contracts\Foundation\ExceptionRenderer; | ||||||
use Illuminate\Contracts\Support\Responsable; | ||||||
use Illuminate\Database\Eloquent\InvalidIdFormatException; | ||||||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||||||
use Illuminate\Database\MultipleRecordsFoundException; | ||||||
use Illuminate\Database\RecordNotFoundException; | ||||||
|
@@ -146,6 +147,7 @@ class Handler implements ExceptionHandlerContract | |||||
BackedEnumCaseNotFoundException::class, | ||||||
HttpException::class, | ||||||
HttpResponseException::class, | ||||||
InvalidIdFormatException::class, | ||||||
ModelNotFoundException::class, | ||||||
MultipleRecordsFoundException::class, | ||||||
RecordNotFoundException::class, | ||||||
|
@@ -634,6 +636,7 @@ protected function prepareException(Throwable $e) | |||||
return match (true) { | ||||||
$e instanceof BackedEnumCaseNotFoundException => new NotFoundHttpException($e->getMessage(), $e), | ||||||
$e instanceof ModelNotFoundException => new NotFoundHttpException($e->getMessage(), $e), | ||||||
$e instanceof InvalidIdFormatException => new HttpException(422, $e->getMessage(), $e), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use
Suggested change
|
||||||
$e instanceof AuthorizationException && $e->hasStatus() => new HttpException( | ||||||
$e->status(), $e->response()?->message() ?: (Response::$statusTexts[$e->status()] ?? 'Whoops, looks like something went wrong.'), $e | ||||||
), | ||||||
|
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.
Wouldn't it make sense to extend the exception from
\Symfony\Component\HttpKernel\Exception\HttpException
then?