On Wed, Feb 28, 2018 at 07:42:51AM +0000, Eric Wong wrote:
> > > > a) We could override the meaning of die() in Git.pm. This feels
> > > > ugly but if it works, it would be a very small patch.
> > >
> > > Unlikely to work since I think we use eval {} to trap exceptions
> > > from die.
> > >
> > > > b) We could forbid use of die() and use some git_die() instead (but
> > > > with a better name) for our own error handling.
> > >
> > > Call sites may be dual-use: "die" can either be caught by an
> > > eval or used to show an error message to the user.
>
> <snip>
>
> > > > d) We could wrap each command in an eval {...} block to convert the
> > > > result from die() to exit 128.
> > >
> > > I prefer option d)
> >
> > FWIW, I agree with all of that. You can do (d) without an enclosing eval
> > block by just hooking the __DIE__ handler, like:
> >
> > $SIG{__DIE__} = sub {
> > print STDERR "fatal: @_\n";
> > exit 128;
> > };
>
> Looks like it has the same problems I pointed out with a) and b).
You're right. I cut down my example too much and dropped the necessary
eval magic. Try this:
-- >8 --
SIG{__DIE__} = sub {
CORE::die @_ if $^S || !defined($^S);
print STDERR "fatal: @_";
exit 128;
};
eval {
die "inside eval";
};
print "eval status: $@" if $@;
die "outside eval";
-- 8< --
Running that should produce:
$ perl foo.pl; echo $?
eval status: inside eval at foo.pl line 8.
fatal: outside eval at foo.pl line 12.
128
It may be getting a little too black-magic, though. Embedding in an eval
is at least straightforward, if a bit more invasive.
-Peff