removed unrequired explicit 'list' data levels
[scpubgit/System-Introspector.git] / lib / System / Introspector / Util.pm
index 98d2429..530f9f9 100644 (file)
@@ -6,6 +6,7 @@ use IPC::Run qw( run );
 use IPC::Open2;
 use File::Spec;
 use Scalar::Util qw( blessed );
+use Capture::Tiny qw( capture_stderr );
 
 our @EXPORT_OK = qw(
     handle_from_command
@@ -43,7 +44,7 @@ sub transform_exceptions (&) {
     my ($code) = @_;
     my $result = eval { $code->() };
     if (my $error = $@) {
-        return { error => $error->message }
+        return { __error__ => $error->message }
             if is_report_exception $error;
         die $@;
     }
@@ -78,11 +79,20 @@ sub handle_from_command {
     local $@;
     my $ok = eval {
         my $out;
-        my $child_pid = open2($out, File::Spec->devnull, $command);
-        my @lines = <$out>;
-        waitpid $child_pid, 0;
+        my $child_pid;
+        my @lines;
+        my ($err) = capture_stderr {
+          $child_pid = open2($out, File::Spec->devnull, $command);
+          @lines = <$out>;
+          close $out;
+          waitpid $child_pid, 0;
+        };
         my $content = join '', @lines;
         my $status = $? >> 8;
+        $err = "Unknown error"
+            unless defined $err;
+        fail "Command error ($command): $err\n"
+            if $status;
         open $pipe, '<', \$content;
         1;
     };