Return body on request exception instead of throwing TelegramException.

Save raw JSON response to ServerResponse object.
parent 1ff9b4f9
...@@ -40,6 +40,7 @@ class ServerResponse extends Entity ...@@ -40,6 +40,7 @@ class ServerResponse extends Entity
*/ */
public function __construct(array $data, $bot_name) public function __construct(array $data, $bot_name)
{ {
$this->raw_json = isset($data['raw_json']) ? $data['raw_json'] : null;
if (isset($data['ok'], $data['result'])) { if (isset($data['ok'], $data['result'])) {
if (is_array($data['result'])) { if (is_array($data['result'])) {
if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) { if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) {
......
...@@ -223,14 +223,14 @@ class Request ...@@ -223,14 +223,14 @@ class Request
if ($action === 'getUpdates') { if ($action === 'getUpdates') {
TelegramLog::update($result); TelegramLog::update($result);
} }
return $result;
} catch (RequestException $e) { } catch (RequestException $e) {
throw new TelegramException($e->getMessage()); $result = (string)$e->getResponse()->getBody();
} finally { } finally {
//Logging verbose debug output //Logging verbose debug output
TelegramLog::endDebugLogTempStream("Verbose HTTP Request output:\n%s\n"); TelegramLog::endDebugLogTempStream("Verbose HTTP Request output:\n%s\n");
} }
return $result;
} }
/** /**
...@@ -263,7 +263,7 @@ class Request ...@@ -263,7 +263,7 @@ class Request
return filesize($file_path) > 0; return filesize($file_path) > 0;
} catch (RequestException $e) { } catch (RequestException $e) {
throw new TelegramException($e->getMessage()); return (string)$e->getResponse()->getBody();
} finally { } finally {
//Logging verbose debug output //Logging verbose debug output
TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n"); TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n");
...@@ -313,12 +313,14 @@ class Request ...@@ -313,12 +313,14 @@ class Request
self::ensureNonEmptyData($data); self::ensureNonEmptyData($data);
$response = json_decode(self::execute($action, $data), true); $raw_json = self::execute($action, $data);
$response = json_decode($raw_json, true);
if (null === $response) { if (null === $response) {
throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.'); throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.');
} }
$response['raw_json'] = $raw_json;
return new ServerResponse($response, $bot_name); return new ServerResponse($response, $bot_name);
} }
...@@ -574,10 +576,6 @@ class Request ...@@ -574,10 +576,6 @@ class Request
*/ */
public static function getUserProfilePhotos(array $data) public static function getUserProfilePhotos(array $data)
{ {
if (!isset($data['user_id'])) {
throw new TelegramException('User id is empty!');
}
return self::send('getUserProfilePhotos', $data); return self::send('getUserProfilePhotos', $data);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment