Skip to content

Commit 56d00a1

Browse files
committed
Add custom exceptions for Telegram errors
1 parent df6a4fb commit 56d00a1

6 files changed

Lines changed: 64 additions & 21 deletions

File tree

src/DB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ public static function insertEditedMessageRequest(Message &$edited_message)
837837
$from_id = $from->getId();
838838

839839
// Don't save the messages edited by myself
840-
if ($from_id == self::telegram->getBotUserId()) {
840+
if ($from_id == self::$telegram->getBotUserId()) {
841841
return;
842842
}
843843

src/Entities/Chat.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Chat extends Entity
2020
protected $username;
2121
protected $first_name;
2222
protected $last_name;
23+
protected $slow_mode_delay;
2324

2425
/**
2526
* Chat constructor.
@@ -49,6 +50,7 @@ public function __construct(array $data)
4950
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
5051
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
5152
$this->username = isset($data['username']) ? $data['username'] : null;
53+
$this->slow_mode_delay = isset($data['slow_mode_delay']) ? $data['slow_mode_delay'] : null;
5254
}
5355

5456
public function isGroupChat()
@@ -113,6 +115,11 @@ public function getUsername()
113115
return $this->username;
114116
}
115117

118+
public function getSlowModeDelay()
119+
{
120+
return $this->slow_mode_delay;
121+
}
122+
116123
public function tryMention()
117124
{
118125
if ($this->isPrivateChat()) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Longman\TelegramBot\Exception\Request;
4+
5+
/**
6+
* Main exception class used for request exception handling
7+
*/
8+
class RequestException extends \Exception
9+
{
10+
/**
11+
* parameters
12+
*
13+
* @var array
14+
*/
15+
protected $parameters = [];
16+
17+
// Redefine the exception so message isn't optional
18+
public function __construct($message, $code = 0, Exception $previous = null)
19+
{
20+
$data = json_decode($message, true);
21+
$this->parameters = $data['parameters'];
22+
//$data['description']
23+
// make sure everything is assigned properly
24+
parent::__construct($data['description'], $code, $previous);
25+
}
26+
27+
/**
28+
* Get the parameters
29+
*
30+
* @param string $key
31+
*
32+
* @return mixed
33+
*/
34+
public function getParameters($key)
35+
{
36+
return $this->parameters[$key];
37+
}
38+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Longman\TelegramBot\Exception\Request;
4+
5+
/**
6+
* Main exception class used for request exception handling
7+
*/
8+
class TooManyRequests extends RequestException
9+
{
10+
}

src/Exception/TelegramSlowModeException.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Request.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
namespace Longman\TelegramBot;
1212

1313
use GuzzleHttp\Client;
14-
use GuzzleHttp\Exception\RequestException;
1514
use Longman\TelegramBot\Entities\File;
1615
use Longman\TelegramBot\Entities\ServerResponse;
1716
use Longman\TelegramBot\Exception\TelegramException;
18-
use Longman\TelegramBot\Exception\TelegramSlowModeException;
17+
use Longman\TelegramBot\Exception\Request\RequestException;
18+
use Longman\TelegramBot\Exception\Request\TooManyRequests;
1919

2020
class Request
2121
{
@@ -208,16 +208,15 @@ public static function execute($action, array $data = null)
208208
'/bot' . self::$telegram->getApiKey() . '/' . $action,
209209
$request_params
210210
);
211-
} catch (RequestException $e) {
212-
//throw new TelegramException($e->getMessage());
211+
} catch (\GuzzleHttp\Exception\RequestException $e) {
213212
$error_code = $e->getResponse()->getStatusCode();
214213
if ($error_code == 429) {
215214
//echo Psr7\Message::toString($e->getRequest());
216215
//echo Psr7\Message::toString($e->getResponse());
217216
//print($e);
218-
throw new TelegramSlowModeException($e->getResponse()->getBody()->getContents(), $error_code);
217+
throw new TooManyRequests($e->getResponse()->getBody()->getContents(), $error_code);
219218
}
220-
throw new TelegramException($e->getResponse()->getBody()->getContents(), $error_code);
219+
throw new RequestException($e->getResponse()->getBody()->getContents(), $error_code);
221220
} finally {
222221
//Logging verbose debug output
223222
TelegramLog::endDebugLogTempStream("Verbose HTTP Request output:\n%s\n");
@@ -259,14 +258,13 @@ public static function downloadFile(File $file)
259258
'/file/bot' . self::$telegram->getApiKey() . '/' . $path,
260259
['debug' => $debug_handle, 'sink' => $loc_path]
261260
);
262-
} catch (RequestException $e) {
263-
//throw new TelegramException($e->getMessage());
264-
throw new TelegramException($e->getResponse()->getBody()->getContents());
261+
} catch (\GuzzleHttp\Exception\RequestException $e) {
262+
$error_code = $e->getResponse()->getStatusCode();
263+
throw new RequestException($e->getResponse()->getBody()->getContents(), $error_code);
265264
} finally {
266265
//Logging verbose debug output
267266
TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n");
268267
}
269-
270268
return (filesize($loc_path) > 0);
271269
}
272270

0 commit comments

Comments
 (0)