diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2004-12-12 11:26:17 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2004-12-12 11:26:17 +0000 |
commit | 79db8ddbe476270aefaa8b2998affdbdb4832410 (patch) | |
tree | d042160fad019954af030a71ba763f490b8dc6c6 | |
parent | 113f55f3830775e56b5a626476cf9c4e606cd57b (diff) |
add somewhat more regular error catching code.
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Add.pm | 11 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Delete.pm | 11 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Error.pm | 82 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Update.pm | 11 | ||||
-rw-r--r-- | usr.sbin/pkg_add/pkg_add | 63 | ||||
-rw-r--r-- | usr.sbin/pkg_add/pkg_create | 26 | ||||
-rw-r--r-- | usr.sbin/pkg_add/pkg_delete | 35 | ||||
-rw-r--r-- | usr.sbin/pkg_add/pkg_info | 57 |
8 files changed, 188 insertions, 108 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/Add.pm b/usr.sbin/pkg_add/OpenBSD/Add.pm index 6b83188403b..c3b1046220c 100644 --- a/usr.sbin/pkg_add/OpenBSD/Add.pm +++ b/usr.sbin/pkg_add/OpenBSD/Add.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Add.pm,v 1.23 2004/12/02 00:37:54 espie Exp $ +# $OpenBSD: Add.pm,v 1.24 2004/12/12 11:26:16 espie Exp $ # # Copyright (c) 2003-2004 Marc Espie <espie@openbsd.org> # @@ -34,10 +34,11 @@ sub manpages_index if ($state->{not}) { print "Merging manpages in $destdir$k: ", join(@l), "\n"; } else { - eval { OpenBSD::Makewhatis::merge($destdir.$k, \@l); }; - if ($@) { - print STDERR "Error in makewhatis: $@\n"; - } + try { + OpenBSD::Makewhatis::merge($destdir.$k, \@l); + } catchall { + print STDERR "Error in makewhatis: $_\n"; + }; } } } diff --git a/usr.sbin/pkg_add/OpenBSD/Delete.pm b/usr.sbin/pkg_add/OpenBSD/Delete.pm index 42388d097c5..8c713d5051f 100644 --- a/usr.sbin/pkg_add/OpenBSD/Delete.pm +++ b/usr.sbin/pkg_add/OpenBSD/Delete.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Delete.pm,v 1.14 2004/12/02 00:37:55 espie Exp $ +# $OpenBSD: Delete.pm,v 1.15 2004/12/12 11:26:16 espie Exp $ # # Copyright (c) 2003-2004 Marc Espie <espie@openbsd.org> # @@ -134,10 +134,11 @@ sub delete_plist print "remove dependency on $name\n" if $state->{very_verbose} or $state->{not}; local $@; - eval { OpenBSD::RequiredBy->new($name)->delete($pkgname) unless $state->{not}; }; - if ($@) { - print STDERR "$@\n"; - } + try { + OpenBSD::RequiredBy->new($name)->delete($pkgname) unless $state->{not}; + } catchall { + print STDERR "$_\n"; + }; $removed->{$name} = 1; }; diff --git a/usr.sbin/pkg_add/OpenBSD/Error.pm b/usr.sbin/pkg_add/OpenBSD/Error.pm index daffc2e52c9..32d08ebc662 100644 --- a/usr.sbin/pkg_add/OpenBSD/Error.pm +++ b/usr.sbin/pkg_add/OpenBSD/Error.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Error.pm,v 1.8 2004/12/06 12:35:36 espie Exp $ +# $OpenBSD: Error.pm,v 1.9 2004/12/12 11:26:16 espie Exp $ # # Copyright (c) 2004 Marc Espie <espie@openbsd.org> # @@ -20,7 +20,8 @@ use warnings; package OpenBSD::Error; our @ISA=qw(Exporter); -our @EXPORT=qw(System VSystem Copy Fatal Warn Usage set_usage); +our @EXPORT=qw(System VSystem Copy Fatal Warn Usage set_usage + try throw catch catchall rethrow); sub System { @@ -61,7 +62,7 @@ sub Copy sub Fatal { require Carp; - Carp::croak @_; + Carp::croak "Expected: @_"; } sub Warn @@ -94,7 +95,11 @@ sub fatal { my $self = shift; require Carp; - Carp::croak ($self->{pkgname}, ':', @_); + if (defined $self->{pkgname}) { + Carp::croak("Expected: ", $self->{pkgname}, ':', @_); + } else { + Carp::croak("Expected: ", @_); + } } sub print @@ -143,7 +148,7 @@ sub Usage { my $code = 0; if (@_) { - print STDERR "Error: ", @_, "\n"; + print STDERR "$0: ", @_, "\n"; $code = 1; } print STDERR "Usage: ", shift(@usage_line), "\n"; @@ -153,4 +158,71 @@ sub Usage exit($code); } +sub dienow +{ + my ($error, $handler) = @_; + if ($error) { + if ($error =~ m/^(Expected:\s+)?(.*?)(?:\s+at\s+(.*)\s+line\s+(\d+)\.?)?$/) { + local $_ = $2; + $OpenBSD: Error.pm,v 1.9 2004/12/12 11:26:16 espie Exp $3; + $OpenBSD: Error.pm,v 1.9 2004/12/12 11:26:16 espie Exp $4; + $OpenBSD: Error.pm,v 1.9 2004/12/12 11:26:16 espie Exp $error; + + $handler->exec($error, $1, $2, $3, $4); + } else { + die "Fatal error: can't parse $error"; + } + } +} + +sub try(&@) +{ + my ($try, $catch) = @_; + eval { &$try }; + dienow($@, $catch); +} + +sub throw +{ + require Carp; + Carp::croak "Expected: @_"; + +} + +sub rethrow +{ + my $e = shift; + die $e if $e; +} + +sub catch(&) +{ + bless $_[0], "OpenBSD::Error::catch"; +} + +sub catchall(&) +{ + bless $_[0], "OpenBSD::Error::catchall"; +} + +our ($FileName, $Line, $FullMessage); + +package OpenBSD::Error::catch; +sub exec +{ + my ($self, $full, $e) = @_; + if ($e) { + &$self; + } else { + die $full; + } +} + +package OpenBSD::Error::catchall; +sub exec +{ + my ($self, $full, $e) = @_; + &$self; +} + 1; diff --git a/usr.sbin/pkg_add/OpenBSD/Update.pm b/usr.sbin/pkg_add/OpenBSD/Update.pm index c4e6e6cca73..802956e522f 100644 --- a/usr.sbin/pkg_add/OpenBSD/Update.pm +++ b/usr.sbin/pkg_add/OpenBSD/Update.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Update.pm,v 1.38 2004/12/02 00:19:26 espie Exp $ +# $OpenBSD: Update.pm,v 1.39 2004/12/12 11:26:16 espie Exp $ # # Copyright (c) 2004 Marc Espie <espie@openbsd.org> # @@ -259,13 +259,12 @@ sub can_do } if ($state->{okay}) { - eval { + try { OpenBSD::Delete::validate_plist($plist, $state); - }; - if ($@) { - Warn "$@"; + } catchall { + Warn "$_"; return 0; - } + }; } $plist->{wantlist} = \@wantlist; diff --git a/usr.sbin/pkg_add/pkg_add b/usr.sbin/pkg_add/pkg_add index f0c39661bb6..53d8379248b 100644 --- a/usr.sbin/pkg_add/pkg_add +++ b/usr.sbin/pkg_add/pkg_add @@ -1,7 +1,7 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: pkg_add,v 1.137 2004/12/09 18:46:00 espie Exp $ +# $OpenBSD: pkg_add,v 1.138 2004/12/12 11:26:15 espie Exp $ # # Copyright (c) 2003-2004 Marc Espie <espie@openbsd.org> # @@ -303,12 +303,13 @@ sub really_add($$) my $donesize = 0; $plist->{done} = []; for my $item (@{$plist->{items}}) { - eval { $item->extract($state); }; - if ($@) { - Warn $@; + try { + $item->extract($state); + } catchall { + Warn $_; $errors++; last; - } + }; push(@{$plist->{done}}, $item); if (defined $item->{size}) { $donesize += $item->{size}; @@ -328,18 +329,17 @@ sub really_add($$) OpenBSD::ProgressMeter::set_header($plist->{replacing}->pkgname()." (deleting)"); $state->set_pkgname($plist->{replacing}->pkgname()); require OpenBSD::Delete; - eval { + try { OpenBSD::Delete::delete_plist($plist->{replacing}, $state); - }; - if ($@) { - Warn $@; + } catchall { + Warn $_; if ($not) { Fatal "Deinstallation of ", $plist->{replacing}->pkgname(), " failed"; } else { OpenBSD::Add::borked_installation($plist, $dir); } - } + }; delete_installed($plist->{replacing}->pkgname()); @@ -355,12 +355,13 @@ sub really_add($$) my $donesize = 0; $state->{end_faked} = 0; for my $item (@{$plist->{groups}}, @{$plist->{users}}, @{$plist->{items}}) { - eval { $item->install($state); }; - if ($@) { - Warn $@; + try { + $item->install($state); } + catchall { + Warn $_; $errors++; last; - } + }; push(@{$plist->{done}}, $item); if (defined $item->{size}) { $donesize += $item->{size}; @@ -378,11 +379,12 @@ sub really_add($$) OpenBSD::ProgressMeter::next(); if (!($interrupted || $errors)) { - eval { do_script($plist, INSTALL, $state, "POST-INSTALL") }; - if ($@) { - Warn $@; + try { + do_script($plist, INSTALL, $state, "POST-INSTALL") + } catchall { + Warn $_; $errors++; - } + }; } unlink($dir.CONTENTS); @@ -535,7 +537,7 @@ sub install_package my $handle = $cache->{$pkg}; if ($errors > 0) { - Fatal "Fatal error" unless defined $handle; + $state->fatal("Fatal error") unless defined $handle; } else { return () unless defined $handle; } @@ -608,20 +610,20 @@ set_usage('pkg_add [-acInqrvvx] [-A arch] [-B pkg-destdir] [-f keys]', our ($opt_a, $opt_v, $opt_n, $opt_I, $opt_L, $opt_B, $opt_A, $opt_P, $opt_Q, $opt_x, $opt_r, $opt_q, $opt_c); $opt_v = 0; -eval { getopts('aqchvnrxIL:f:B:A:P:Q:', +try { + getopts('aqchvnrxIL:f:B:A:P:Q:', {'v' => sub {++$opt_v;}, 'h' => sub { Usage(); }, 'f' => sub { for my $o (split/,/, shift) { $forced{$o} = 1; } - }}); }; -if ($@) { - chomp($@); - $@ =~ s/\s+at.*?$//; - Usage($@); -} + }}); +} catchall { + Usage($_); +}; +try { $opt_L = '/usr/local' unless defined $opt_L; my $state = new OpenBSD::Error; @@ -683,7 +685,7 @@ if ($< && !$forced{nonroot}) { if ($state->{not}) { Warn "$0 should be run as root\n"; } else { - Fatal "$0 must be run as root"; + Fatal "must be run as root"; } } @@ -713,7 +715,8 @@ if ($state->{beverbose}) { OpenBSD::Vstat::tally(); } $state->delayed_output(); -if ($dielater) { - print STDERR $dielater; +rethrow $dielater; +} catch { + print STDERR "$0: $_\n"; exit(1); -} +}; diff --git a/usr.sbin/pkg_add/pkg_create b/usr.sbin/pkg_add/pkg_create index 219f5e3b7c4..46c46c3b465 100644 --- a/usr.sbin/pkg_add/pkg_create +++ b/usr.sbin/pkg_add/pkg_create @@ -1,6 +1,6 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: pkg_create,v 1.30 2004/12/10 12:02:37 jmc Exp $ +# $OpenBSD: pkg_create,v 1.31 2004/12/12 11:26:16 espie Exp $ # # Copyright (c) 2003-2004 Marc Espie <espie@openbsd.org> # @@ -279,7 +279,8 @@ set_usage( '[-U undisplayfile] [-W wantedlib] -c desc -d desc -f packinglist pkgname'); my $plist = new OpenBSD::PackingList; -eval { getopts('hp:f:c:d:vi:k:r:M:U:S:hs:OA:L:B:D:P:W:n', +try { + getopts('hp:f:c:d:vi:k:r:M:U:S:hs:OA:L:B:D:P:W:n', {'D' => sub { local $_ = shift; @@ -300,12 +301,10 @@ eval { getopts('hp:f:c:d:vi:k:r:M:U:S:hs:OA:L:B:D:P:W:n', 'W' => sub { OpenBSD::PackingElement::Wantlib->add($plist, shift); } - }); }; -if ($@) { - chomp($@); - $@ =~ s/\s+at.*?$//; - Usage($@); -} + }); +} catchall { + Usage($_); +}; if (@ARGV == 0) { $regen_package = 1; @@ -313,6 +312,7 @@ if (@ARGV == 0) { Usage "Exactly one single package name is required"; } +try { my $dir = OpenBSD::Temp::dir(); my $dir2 = $dir; @@ -448,7 +448,7 @@ for my $contentsfile (@contents) { } } } - ) or die "Can't open packing list $contentsfile"; + ) or Fatal "Can't open packing list $contentsfile"; } if (!$plist->has('name') && $ARGV[0] =~ m|([^/]+)$|) { @@ -510,7 +510,7 @@ my $wname; if ($regen_package) { $wname = $plist->pkgname().".tgz"; } else { - $plist->tofile($dir.CONTENTS) or die "Can't write packing list"; + $plist->tofile($dir.CONTENTS) or Fatal "Can't write packing list"; $wname = $ARGV[0]; } @@ -522,5 +522,9 @@ if ($opt_n) { exit(0); } else { print "Creating gzip'd tar ball in '$wname'\n" if $opt_v; - System(@cmd) == 0 or die "tar failed"; + System(@cmd) == 0 or Fatal "tar failed"; } +} catch { + print STDERR "$0: $_\n"; + exit(1); +}; diff --git a/usr.sbin/pkg_add/pkg_delete b/usr.sbin/pkg_add/pkg_delete index 4aabf1e6735..5bcc27f5428 100644 --- a/usr.sbin/pkg_add/pkg_delete +++ b/usr.sbin/pkg_add/pkg_delete @@ -1,6 +1,6 @@ #!/usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: pkg_delete,v 1.80 2004/12/06 12:35:36 espie Exp $ +# $OpenBSD: pkg_delete,v 1.81 2004/12/12 11:26:16 espie Exp $ # # Copyright (c) 2003-2004 Marc Espie <espie@openbsd.org> # @@ -34,19 +34,18 @@ our ($opt_v, $opt_D, $opt_d, $opt_n, $opt_q, $opt_p, $opt_c, $opt_L, $opt_B, $op $opt_v = 0; -eval { getopts('vchxDdnf:qpS:L:B:I', - {'v' => sub {++$opt_v;}, - 'h' => sub { Usage(); }, - 'f' => sub { - for my $o (split/,/, shift) { - $forced{$o} = 1; - } - }}); }; -if ($@) { - chomp($@); - $@ =~ s/\s+at.*?$//; - Usage($@); -} +try { + getopts('vchxDdnf:qpS:L:B:I', + {'v' => sub {++$opt_v;}, + 'h' => sub { Usage(); }, + 'f' => sub { + for my $o (split/,/, shift) { + $forced{$o} = 1; + } + }}); +} catchall { + Usage($_); +}; if ($opt_D) { $opt_I = 1; @@ -70,6 +69,7 @@ if (defined $opt_d) { my %done; my $removed; +try { my $state = new OpenBSD::Error; $state->{not} = $opt_n; $state->{quick} = $opt_q; @@ -211,7 +211,8 @@ if ($state->{beverbose}) { OpenBSD::Vstat::tally(); } $state->delayed_output(); -if ($dielater) { - print STDERR $dielater; +rethrow $dielater; +} catch { + print STDERR "$0: $_\n"; exit(1); -} +}; diff --git a/usr.sbin/pkg_add/pkg_info b/usr.sbin/pkg_add/pkg_info index d2cecacfda8..6eeb6e83215 100644 --- a/usr.sbin/pkg_add/pkg_info +++ b/usr.sbin/pkg_add/pkg_info @@ -1,6 +1,6 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: pkg_info,v 1.23 2004/12/06 15:19:11 espie Exp $ +# $OpenBSD: pkg_info,v 1.24 2004/12/12 11:26:16 espie Exp $ # # Copyright (c) 2003-2004 Marc Espie <espie@openbsd.org> # @@ -246,36 +246,35 @@ sub print_info set_usage('pkg_info [-cDdfIiKkLMpqRrsUv] [-E fname] [-e pkgname] [-l str] pkgname...', 'pkg_info [-Aa flags]'); -eval { getopts('cDdfhIikKLmpqRrsvhe:E:MU:l:aA', - {'e' => - sub { - my $pat = shift; - my @list; - if ($pat =~ m/\//) { - @list = find_by_path($pat); - } else { - @list = find_by_spec($pat); - } - if (@list == 0) { - $exit_code = 1; - } - push(@ARGV, @list); - $terse = 1; - }, - 'h' => sub { Usage(); }, - 'E' => - sub { - require File::Spec; +try { + getopts('cDdfhIikKLmpqRrsvhe:E:MU:l:aA', + {'e' => + sub { + my $pat = shift; + my @list; + if ($pat =~ m/\//) { + @list = find_by_path($pat); + } else { + @list = find_by_spec($pat); + } + if (@list == 0) { + $exit_code = 1; + } + push(@ARGV, @list); + $terse = 1; + }, + 'h' => sub { Usage(); }, + 'E' => + sub { + require File::Spec; - push(@sought_files, File::Spec->rel2abs(shift)); + push(@sought_files, File::Spec->rel2abs(shift)); - } - }); }; -if ($@) { - chomp($@); - $@ =~ s/\s+at.*?$//; - Usage($@); -} + } + }); +} catchall { + Usage($_); +}; if ($opt_D) { $opt_M = 1; |