Yii2: Console ErrorHandler does not set correct exit code / return value

Created on 22 Nov 2017  路  3Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

Create a console command that throws an exception:
public function actionFail() { throw new \Exception("An exception is not ideal"); }

What is the expected result?

A non-zero return value.

What do you get instead?

A zero return value (https://github.com/yiisoft/yii2/blob/master/framework/base/ErrorHandler.php#L112)
... if (!YII_ENV_TEST) { \Yii::getLogger()->flush(true); if (defined('HHVM_VERSION')) { flush(); } exit(1); }

To me it would make more sense to always return exit code 1 in case we are handling an exception, no matter the environment.

under discussion

Most helpful comment

Disagreed.
The code block you refer prevents application termination in case of exception in test mode.
Console application is often used during unit test running. Enforcing exit on error will cause it to terminate PHPUnit script.

All 3 comments

Disagreed.
The code block you refer prevents application termination in case of exception in test mode.
Console application is often used during unit test running. Enforcing exit on error will cause it to terminate PHPUnit script.

Agree with @klimov-paul. The reason for handing it differently is running tests for commands.

Hi!
I've read carefully this and all similar issues. And I understand why behavior works so.
I don't ask to change it permanently. I suggest to provide possibility to manage this behavior.
Because it is problem, if you are running some console command while Continuous Integration.

Real Example:

  • this Travis build marked as passed. But really there was a bug - fixtures were not applied because one of them crashed:
  • that's why CI can't know, that Error occurred - problem

Additionally
I've tried to workaround this problem with ExitException: throw \yii\base\ExitException(1, 'error', 1);. But exit code still 0.

Was this page helpful?
0 / 5 - 0 ratings