Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklul committed Jun 22, 2018
1 parent 2fcef26 commit e0469c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
16 changes: 9 additions & 7 deletions src/E621.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class E621
*
* @const string
*/
const VERSION = '0.4.0';
const VERSION = '0.4.1';

/**
* Base URL for API calls
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
19 changes: 13 additions & 6 deletions src/Entity/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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'],
Expand All @@ -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' => [],
Expand Down

0 comments on commit e0469c8

Please sign in to comment.