StevieD
StevieD

Reputation: 7443

How do I prevent script from crashing as a result of failed proc?

I've got this:

try { run 'tar', '-zxvf', $path.Str, "$dir/META6.json", :err }

Despite being in a try{} block, this line is still causing my script to crash:

The spawned command 'tar' exited unsuccessfully (exit code: 1, signal: 0)
  in block  at ./all-versions.raku line 27
  in block  at ./all-versions.raku line 16
  in block <unit> at ./all-versions.raku line 13

Why isn't the try{} block allowing the script to continue and how can I get it to continue?

Upvotes: 7

Views: 110

Answers (1)

Elizabeth Mattijsen
Elizabeth Mattijsen

Reputation: 26969

That's because the run didn't fail (yet). run returns a Proc object. And that by itself doesn't throw (yet).

try just returns that Proc object. As soon as the returned value is used however (for instance, by having it sunk), then it will throw.

Compare (with immediate sinking):

$ raku -e 'run "foo"'
The spawned command 'foo' exited unsuccessfully (exit code: 1, signal: 0)

with:

$ raku -e 'my $a = run "foo"; say "ran, going to sink"; $a.sink'
ran, going to sink
The spawned command 'foo' exited unsuccessfully (exit code: 1, signal: 0)

Now, what causes the usage of the Proc object in your code, is unclear. You'd have to show more code.

A way to check for success, is to check the exit-code:

$ raku -e 'my $a = run "foo"; say "failed" if $a.exitcode > 0'
failed

$ raku -e 'my $a = run "echo"; say "failed" if $a.exitcode > 0'

Or alternately, use Jonathan's solution:

$ raku -e 'try sink run "foo"'

Upvotes: 5

Related Questions