You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At the moment many Eloquent methods report errors inconsistently. (Probably, this is also true for Laravel in general, but I mostly use Eloquent and there it is specifically striking.)
For example, the methods \Illuminate\Database\Eloquent\Model::save() and \Illuminate\Database\Eloquent\Model::delete() return a nullable boolean and may throw exceptions. If save returns true or null, this indicates a success. If it returns false, this indicates a failure (without further information what went wrong). However, save may also throw exceptions.
To properly handle all error cases in one place, I typically write something like this
try {
if ($model->save() === false) {
// sic! we must explicitly check for === false, because save() may also return null on success
throw new \RuntimeException('Model::save() returned false');
}
} catch (\Throwable $e) {
// ... error handling in one place
}
First, I would really like if Laravel would be consistent: either use a boolean return value to indicate success/fail or use exceptions, but not both at the same time. This should also be consistent across the whole API.
Second, I would recommend to use exceptions. Exceptions are around since PHP 5, I believe they are the "modern" way of error reporting and they allow to differentiate between different error cases. I would love to write something like that
try {
$model->save();
} catch (EloquentError $e) {
// something on the DB layer went wrong
}
} catch (InteruptedByObserver $e) {
// some observer listing to the saving event decided to not save this model
}
} catch (\AbstractLaravelError $e) {
// oops, something went completely sideways
}
This would mean that many methods (like save, delete, update) which return a boolean now would return void in the future.
Note: This idea is related to discussion #40020 as the above example would also imply that Laravel only throws specific exceptions.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
At the moment many Eloquent methods report errors inconsistently. (Probably, this is also true for Laravel in general, but I mostly use Eloquent and there it is specifically striking.)
For example, the methods
\Illuminate\Database\Eloquent\Model::save()
and\Illuminate\Database\Eloquent\Model::delete()
return a nullable boolean and may throw exceptions. Ifsave
returnstrue
ornull
, this indicates a success. If it returnsfalse
, this indicates a failure (without further information what went wrong). However,save
may also throw exceptions.To properly handle all error cases in one place, I typically write something like this
First, I would really like if Laravel would be consistent: either use a boolean return value to indicate success/fail or use exceptions, but not both at the same time. This should also be consistent across the whole API.
Second, I would recommend to use exceptions. Exceptions are around since PHP 5, I believe they are the "modern" way of error reporting and they allow to differentiate between different error cases. I would love to write something like that
This would mean that many methods (like
save
,delete
,update
) which return a boolean now would returnvoid
in the future.Note: This idea is related to discussion #40020 as the above example would also imply that Laravel only throws specific exceptions.
Beta Was this translation helpful? Give feedback.
All reactions