nategood / commando

An Elegant CLI Library for PHP
MIT License
798 stars 80 forks source link

Errors do not bubble up... #68

Open cwbeck opened 8 years ago

cwbeck commented 8 years ago
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

NeoVance commented 7 years ago

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.

mrcnpdlk commented 7 years ago

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.

NeoVance commented 7 years ago

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.