From 67c1123d7e7dd355f9a238bdb25c79214bd7eef9 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sun, 5 Feb 2017 05:39:13 +0100 Subject: [PATCH] Improve error reporting for reddit spout Closes: #633 --- spouts/reddit/reddit2.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/spouts/reddit/reddit2.php b/spouts/reddit/reddit2.php index 5814b95b8e..8735f7767f 100644 --- a/spouts/reddit/reddit2.php +++ b/spouts/reddit/reddit2.php @@ -129,7 +129,7 @@ public function load( $params ) { if (!empty($params['password']) && !empty($params['username'])) { if (function_exists("apc_fetch")) { - $this->reddit_session = apc_fetch("{$params['username']}_slefoss_reddit_session"); + $this->reddit_session = apc_fetch("{$params['username']}_selfoss_reddit_session"); if (empty($this->reddit_session)) { $this->login($params); } @@ -137,7 +137,17 @@ public function load( $params ) { $this->login($params); } } + $json = json_decode($this->file_get_contents_curl("https://www.reddit.com/" . $params['url'] . ".json")); + + if ($json === null) { + throw new \Exception("Cannot parse the response."); + } + + if (isset($json->error)) { + throw new \Exception($json->message); + } + $this->items = $json->data->children; } @@ -480,11 +490,15 @@ private function login($params) $response = curl_exec($ch); $response = json_decode($response); if (curl_errno($ch)) { - print(curl_error($ch)); + throw new \Exception(curl_error($ch)); } else { curl_close($ch); if (count($response->json->errors) > 0){ - print($response); + $errors = ''; + foreach ($response->json->errors as $error) { + $errors .= $error[1] . PHP_EOL; + } + throw new \Exception($errors); } else { $this->reddit_session = "reddit_session={$response->json->data->cookie}"; if (function_exists("apc_store")) {