[ 11.x ] Adds ability to manually fail a command from outside the handle() method #51435
+103
−3
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.
In a large and complex artisan command where smaller bits are extracted into separate methods (each of which is a possible failure point), we have a couple of options to exit with a non-zero exit code without dumping the entire Exception.
We can wrap the method call in a
try/catch
, and return in the catch block.We can use a return value and wrap it in an if block.
These obviously work just fine, but it's a paper cut I've run into many times. The Queue has a
$this->fail()
convenience method, so I've implemented one on theCommand
class to mimic it. This new approach feels more readable, and more intuitive. Since we're only working with a custom Exception, backwards compatibility is fully maintained. Here's an example of this new method of failing a command outside of thehandle()
method: