commando icon indicating copy to clipboard operation
commando copied to clipboard

Errors do not bubble up...

Open cwbeck opened this issue 9 years ago • 3 comments

try {
  throw new \Exception('Expect to see error caught');
} catch (\Throwable $t) {
  var_dump($t->getMessage());
}

But in your library...

$cmd = CMD::getInstance()->doNotTrapErrors();
try {
  $cmd->option('r')
    ->require()
    ->aka('run')
    ->describedAs('When set, use this title to address the person');
} catch (\Throwable $t) {
  var_dump('caught');
}

Never gets to that statement var_dump('caught');

Instead it just dumps out the error and dies...

PHP Fatal error:  Uncaught Exception: Required option r must be specified in /var/www/nano/vendor/nategood/commando/src/Commando/Command.php:466
Stack trace:
#0 /var/www/nano/vendor/nategood/commando/src/Commando/Command.php(164): Commando\Command->parse()
#1 [internal function]: Commando\Command->__destruct()
#2 {main}
  thrown in /var/www/nano/vendor/nategood/commando/src/Commando/Command.php on line 466

Fatal error: Uncaught Exception: Required option r must be specified in /var/www/nano/vendor/nategood/commando/src/Commando/Command.php on line 466

Exception: Required option r must be specified in /var/www/nano/vendor/nategood/commando/src/Commando/Command.php on line 466

Call Stack:
    0.0237     585528   1. Commando\Command->__destruct() /var/www/nano/vendor/nategood/commando/src/Commando/Command.php:0
    0.0237     585528   2. Commando\Command->parse() /var/www/nano/vendor/nategood/commando/src/Commando/Command.php:164
    0.0239     589064   3. Commando\Command->error() /var/www/nano/vendor/nategood/commando/src/Commando/Command.php:498

Too add:

   public function run(){
      if (!$this->parsed) {
          $this->parse();
      }
    }

    public function __destruct()
    {
        if (!$this->parsed) {
            $this->parse();
        }
    }

Created an identical function run(), when called (in sequence) the error is now caught properly. Is the a PHP issue whereby thrown \Exception after destruct? No idea...

try {
  $cmd = CMD::getInstance()->doNotTrapErrors();
  $cmd->option('r')
    ->require()
    ->aka('run')
    ->describedAs('When set, use this title to address the person')
    ->run();
} catch (\Throwable $t) {
  var_dump('now gets here when run() is used');
}

Fixed in #69

cwbeck avatar Oct 28 '16 14:10 cwbeck

You can use the parse method, or move your try block around the the code where commando is being used, rather than where the command is being defined.

NeoVance avatar Nov 13 '16 10:11 NeoVance

PHP doc says: "Attempting to throw an exception from a destructor (called in the time of script termination) causes a fatal error." So you cannot throw exception here. Of course You can, but dontcatch.

mrcnpdlk avatar Jan 31 '17 23:01 mrcnpdlk

Right. If you don't explicitly run your Command, you can't catch the errors since validation wont happen until script termination. That is exactly how it works.

NeoVance avatar Jul 26 '17 16:07 NeoVance