From: Robert 'phaylon' Sedlacek Date: Fri, 11 May 2012 21:57:40 +0000 (+0000) Subject: only wrap our own errors X-Git-Tag: v0.001_001~67 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c23ab9731e4dc7546c95fc069047b153f17821fe;p=scpubgit%2FSystem-Introspector.git only wrap our own errors --- diff --git a/lib/System/Introspector/Util.pm b/lib/System/Introspector/Util.pm index 7be2fc1..5943490 100644 --- a/lib/System/Introspector/Util.pm +++ b/lib/System/Introspector/Util.pm @@ -14,11 +14,20 @@ our @EXPORT_OK = qw( transform_exceptions ); +do { + package System::Introspection::_Exception; + use Moo; + has message => (is => 'ro'); +}; + +sub fail { die System::Introspection::_Exception->new(message => shift) } +sub is_report_exception { ref(shift) eq 'System::Introspection::_Exception' } + sub files_from_dir { my ($dir) = @_; my $dh; opendir $dh, $dir - or die "Unable to read directory $dir: $!\n"; + or fail "Unable to read directory $dir: $!"; my @files; while (defined( my $item = readdir $dh )) { next if -d "$dir/$item"; @@ -29,10 +38,12 @@ sub files_from_dir { sub transform_exceptions (&) { my ($code) = @_; - local $@; my $result = eval { $code->() }; - return { error => "$@" } - if $@; + if (my $error = $@) { + return { error => $error->message } + if is_report_exception $error; + die $@; + } return $result; } @@ -46,7 +57,7 @@ sub output_from_command { if wantarray; $command = join ' ', @$command if ref $command; - die "Error running command ($command): $err\n" + fail "Error running command ($command): $err" unless $ok; return $out; } @@ -61,14 +72,14 @@ sub lines_from_command { sub handle_from_command { my ($command) = @_; open my $pipe, '-|', $command - or die "Unable to read from command '$command': $!\n"; + or fail "Unable to read from command '$command': $!"; return $pipe; } sub handle_from_file { my ($file) = @_; open my $fh, '<', $file - or die "Unable to read $file: $!\n"; + or fail "Unable to read $file: $!"; return $fh; }