Handle HTTP errors when extracting links
[tt-rss-micropub.git] / init.php
index 745d432424a246172747c2af63687408f268b4b1..3d8008fe4dd1ecb87810b42c79c4e4faf22d332d 100644 (file)
--- a/init.php
+++ b/init.php
@@ -225,6 +225,9 @@ class Micropub extends Plugin implements IHandler
 
 
         $links = $this->getLinks($me);
+        if ($links === false) {
+            return $this->errorOut('Error fetching URL: ' . $me);
+        }
         if (!count($links)) {
             return $this->errorOut('No links found');
         }
@@ -272,9 +275,18 @@ class Micropub extends Plugin implements IHandler
         $status = array_shift($headers);
         list($httpver, $code, $text) = explode(' ', $status, 3);
         if ($code != 201 && $code != 202) {
+            $errData = json_decode($content);
+            if (isset($errData->error_description)
+                && $errData->error_description != ''
+            ) {
+                return $this->errorOut(
+                    'Error creating post: '
+                    . $errData->error_description
+                );
+            }
             return $this->errorOut(
-                'An error occured: '
-                . $code . ' ' . $text
+                'Error creating post: '
+                . $code . ' ' . $text.$content
             );
         }
 
@@ -316,6 +328,9 @@ class Micropub extends Plugin implements IHandler
         //step 1: micropub discovery
         $links = $this->getLinks($url);
 
+        if ($links === false) {
+            return $this->errorOut('Error fetching URL: ' . $url);
+        }
         if (!count($links)) {
             return $this->errorOut('No links found');
         }
@@ -356,6 +371,9 @@ class Micropub extends Plugin implements IHandler
         }
 
         $links = $this->getLinks($_GET['me']);
+        if ($links === false) {
+            return $this->errorOut('Error fetching URL: ' . $_GET['me']);
+        }
         if (!isset($links['token_endpoint'])) {
             return $this->errorOut('No token endpoint found');
         }
@@ -530,6 +548,10 @@ class Micropub extends Plugin implements IHandler
 
     /**
      * Extract link relations from a given URL
+     *
+     * @param string $url URL to extract links from
+     *
+     * @return bool|array Array of links, or false on HTTP error
      */
     protected function getLinks($url)
     {
@@ -539,6 +561,10 @@ class Micropub extends Plugin implements IHandler
                 'url' => $url,
             ]
         );
+        if ($html === false) {
+            return false;
+        }
+
         //Loading invalid HTML is tedious.
         // quick hack with regex. yay!
         preg_match_all('#<link[^>]+?>#', $html, $matches);