From e0469c89a324fa680ca9c93fd4b5d87e26bb600a Mon Sep 17 00:00:00 2001 From: Jack'lul Date: Fri, 22 Jun 2018 21:11:32 +0200 Subject: [PATCH] Improve error handling --- src/E621.php | 16 +++++++++------- src/Entity/Response.php | 19 +++++++++++++------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/E621.php b/src/E621.php index 44e53c1..743471b 100644 --- a/src/E621.php +++ b/src/E621.php @@ -152,7 +152,7 @@ class E621 * * @const string */ - const VERSION = '0.4.0'; + const VERSION = '0.4.1'; /** * Base URL for API calls @@ -853,7 +853,10 @@ private function request($path = 'post/index.json', $method = 'GET', array $data $result = json_decode($raw_result, true); if (!is_array($result)) { - $result = ['success' => false, 'reason' => 'Invalid data returned', 'raw_result' => $raw_result]; + $result = [ + 'reason' => 'Data received from e621.net API is invalid', + 'error' => 'Response couldn\'t be decoded into array', + ]; } } catch (RequestException $e) { $this->debugLog($e); @@ -871,11 +874,10 @@ private function request($path = 'post/index.json', $method = 'GET', array $data } if (!is_array($result)) { - $result = ['success' => false, 'reason' => $result]; - - if ($raw_result !== null) { - $result['raw_result'] = $raw_result; - } + $result = [ + 'reason' => 'Connection to e621.net API failed or timed out', + 'error' => $result, + ]; } } finally { $this->endDebugStream(); diff --git a/src/Entity/Response.php b/src/Entity/Response.php index d7c814e..8817170 100644 --- a/src/Entity/Response.php +++ b/src/Entity/Response.php @@ -13,11 +13,12 @@ use InvalidArgumentException; /** - * @method bool getSuccess() Is the request successful? + * @method bool getSuccess() Was the request successful? * @method mixed getResult() Result of the request (usually array containing objects, empty array when no results) * @method string getRawResult() Raw result of the request (usually JSON string) - * @method string getReason() Description of the failed request - * @method string getMessage() long description of the failed request + * @method string getReason() Reason why the request failed (returned by the API) - is safe to be displayed to the user, in case of internal errors this is set to safe to display value + * @method string getMessage() Description of the failure reason (returned by the API) - not always available + * @method string getError() Returns internal error description (timeouts, connection issues) - it is NOT safe to display this to the users (can contain sensitive information) */ class Response extends Entity { @@ -33,10 +34,16 @@ public function __construct($class, array $result = [], $raw_result = '') } if (!is_array($result)) { - throw new InvalidArgumentException('Argument "timeout" must be an array!'); + throw new InvalidArgumentException('Argument "result" must be an array'); } - if (isset($result['success']) && $result['success'] === false) { + if (isset($result['error'])) { // Internal errors + $data = [ + 'success' => false, + 'reason' => $result['reason'], + 'error' => $result['error'], + ]; + } elseif (isset($result['success']) && $result['success'] === false) { // Errors coming from the API $data = [ 'success' => false, 'reason' => $result['reason'], @@ -45,7 +52,7 @@ public function __construct($class, array $result = [], $raw_result = '') if (isset($result['message'])) { $data['message'] = $result['message']; } - } elseif (count($result) === 0) { + } elseif (count($result) === 0) { // Empty results $data = [ 'success' => true, 'result' => [],