Browse Source

Improve error messages when network failures occur

Jordi Boggiano 13 years ago
parent
commit
a54bf6269e

+ 1 - 1
src/Composer/Json/JsonFile.php

@@ -85,7 +85,7 @@ class JsonFile
                 $json = file_get_contents($this->path);
             }
         } catch (TransportException $e) {
-            throw new \RuntimeException('Could not read '.$this->path.', either you or the remote host is probably offline'."\n\n".$e->getMessage());
+            throw new \RuntimeException($e->getMessage());
         } catch (\Exception $e) {
             throw new \RuntimeException('Could not read '.$this->path."\n\n".$e->getMessage());
         }

+ 1 - 0
src/Composer/Repository/ComposerRepository.php

@@ -101,6 +101,7 @@ class ComposerRepository extends ArrayRepository implements NotifiableRepository
             $this->cache->write('packages.json', json_encode($data));
         } catch (\Exception $e) {
             if ($contents = $this->cache->read('packages.json')) {
+                $this->io->write('<warning>'.$e->getMessage().'</warning>');
                 $this->io->write('<warning>'.$this->url.' could not be loaded, package information was loaded from the local cache and may be out of date</warning>');
                 $data = json_decode($contents, true);
             } else {

+ 10 - 2
src/Composer/Util/RemoteFilesystem.php

@@ -101,7 +101,15 @@ class RemoteFilesystem
             $this->io->write("    Downloading: <comment>connection...</comment>", false);
         }
 
-        $result = @file_get_contents($fileUrl, false, $ctx);
+        $errorMessage = null;
+        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.')';
+            }
+        });
+        $result = file_get_contents($fileUrl, false, $ctx);
+        restore_error_handler();
 
         // fix for 5.4.0 https://bugs.php.net/bug.php?id=61336
         if (!empty($http_response_header[0]) && preg_match('{^HTTP/\S+ 404}i', $http_response_header[0])) {
@@ -148,7 +156,7 @@ class RemoteFilesystem
         }
 
         if (false === $this->result) {
-            throw new TransportException('The "'.$fileUrl.'" file could not be downloaded');
+            throw new TransportException('The "'.$fileUrl.'" file could not be downloaded: '.$errorMessage);
         }
     }