Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors do not bubble up... #68

Open
cwbeck opened this issue Oct 28, 2016 · 3 comments
Open

Errors do not bubble up... #68

cwbeck opened this issue Oct 28, 2016 · 3 comments

Comments

@cwbeck
Copy link

cwbeck commented Oct 28, 2016

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
Copy link
Contributor

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
Copy link

mrcnpdlk commented Jan 31, 2017

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants