From 852c3695750a8f8bd6dac3172d3ae40d0a14b3dc Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 18 Oct 2012 10:30:32 +0200 Subject: [PATCH] Improve error reporting on downloads and copies, refs #1228 --- src/Composer/Command/SelfUpdateCommand.php | 2 +- src/Composer/Util/RemoteFilesystem.php | 23 ++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Composer/Command/SelfUpdateCommand.php b/src/Composer/Command/SelfUpdateCommand.php index d37307217..e36c860b8 100644 --- a/src/Composer/Command/SelfUpdateCommand.php +++ b/src/Composer/Command/SelfUpdateCommand.php @@ -61,10 +61,10 @@ EOT unset($phar); rename($tempFilename, $localFilename); } catch (\Exception $e) { + @unlink($tempFilename); if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) { throw $e; } - unlink($tempFilename); $output->writeln('The download is corrupted ('.$e->getMessage().').'); $output->writeln('Please re-run the self-update command to try again.'); } diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 3ccb127ac..d013e3b66 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -103,14 +103,17 @@ class RemoteFilesystem $this->io->write(" Downloading: connection...", false); } - $errorMessage = null; + $errorMessage = ''; set_error_handler(function ($code, $msg) use (&$errorMessage) { - $errorMessage = preg_replace('{^file_get_contents\(.+?\): }', '', $msg); - if (!ini_get('allow_url_fopen')) { - $errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')'; + if ($errorMessage) { + $errorMessage .= "\n"; } + $errorMessage .= preg_replace('{^file_get_contents\(.*?\): }', '', $msg); }); $result = file_get_contents($fileUrl, false, $ctx); + if ($errorMessage && !ini_get('allow_url_fopen')) { + $errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')'; + } restore_error_handler(); // fix for 5.4.0 https://bugs.php.net/bug.php?id=61336 @@ -146,9 +149,17 @@ class RemoteFilesystem // handle copy command if download was successful if (false !== $result && null !== $fileName) { - $result = (bool) @file_put_contents($fileName, $result); + $errorMessage = ''; + set_error_handler(function ($code, $msg) use (&$errorMessage) { + if ($errorMessage) { + $errorMessage .= "\n"; + } + $errorMessage .= preg_replace('{^file_put_contents\(.*?\): }', '', $msg); + }); + $result = (bool) file_put_contents($fileName, $result); + restore_error_handler(); if (false === $result) { - throw new TransportException('The "'.$fileUrl.'" file could not be written to '.$fileName); + throw new TransportException('The "'.$fileUrl.'" file could not be written to '.$fileName.': '.$errorMessage); } }