In a console controller response->setErrorCode is replace by ConsoleModel #2722

Closed
bouchery opened this Issue Oct 10, 2012 · 5 comments

Comments

Projects
None yet
5 participants

When using $this->response->setErrorCode(..) in a console controller, after returning a string for output or a ConsoleModel, errorCode is set to "null" in meta response message.

Property $response is not really usable.

Contributor

ThaDafinser commented Aug 23, 2013

@bouchery is this still a problem?

When yes: could you please provide an example?

Contributor

imel96 commented Nov 26, 2013

vote to close, Zend\Console\Response::setErrorCode() must have been replaced by setErrorLevel() (at least in 2.2.5).

Contributor

samsonasik commented Feb 24, 2014

ping @bouchery ...

Currently, with ZF 2.2.5, in my action, this code :
$this->response->setErrorLevel(-15);
return "An error";

It displayed "An error" in my console, but shell "echo $?" return ... "0", not "-15"

Owner

weierophinney commented Feb 10, 2015

I've figured out why. At some point, we introduced Zend\Mvc\ResponseSender\ConsoleResponseSender, and, at that time, deprecated the usage of Zend\Console\Response::send() — as in, we no longer call it. That method had an exit($errorLevel) call in it which was responsible for setting the exit status code; the "new" response sender does not call exit.

To get the behavior you want, you need to use Zend\View\Model\ConsoleModel:

// Inside your console action method; make sure you import the class!
$output = new ConsoleModel();
$output->setErrorLevel(-15);
$output->setResult("done!");
return $output;

This works correctly. One note: most shells will not deal with negative numbers for statuses, and will instead subtract that number for the maximum status; -15 becomes 241.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment