Better exceptions

This commit is contained in:
Daniil Gentili 2019-06-26 17:16:06 +02:00
parent 6f5d1b16fa
commit 9d86507eb8
7 changed files with 48 additions and 77 deletions

2
docs

@ -1 +1 @@
Subproject commit dc05dc5cebfcec90ac7851928c522a4d635dbab6 Subproject commit cbd913fba2249a0055bb732452a67f911119c40a

View File

@ -74,7 +74,7 @@ class Conversion
{ {
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
if (!extension_loaded('sqlite3')) { if (!extension_loaded('sqlite3')) {
throw new Exception(['extension', 'sqlite3']); throw Exception::extension('sqlite3');
} }
if (!isset(pathinfo($session)['extension'])) { if (!isset(pathinfo($session)['extension'])) {
$session .= '.session'; $session .= '.session';

View File

@ -31,27 +31,8 @@ class Exception extends \Exception
public function __construct($message = null, $code = 0, self $previous = null, $file = null, $line = null) public function __construct($message = null, $code = 0, self $previous = null, $file = null, $line = null)
{ {
if (is_array($message) && $message[0] === 'extension') {
if ($message[1] === 'libtgvoip') {
$additional = 'Follow the instructions @ https://voip.madelineproto.xyz to install it.';
} elseif ($message[1] === 'prime') {
$additional = 'Follow the instructions @ https://prime.madelineproto.xyz to install it.';
} else {
$additional = 'Try running sudo apt-get install php'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'-'.$message[1].'.';
}
$message = 'MadelineProto requires the '.$message[1].' extension to run. '.$additional;
if (php_sapi_name() !== 'cli') {
echo $message.'<br>';
}
$file = 'MadelineProto';
$line = 1;
}
$this->prettify_tl(); $this->prettify_tl();
if ($file !== null) { if ($file !== null) {
if (basename($file) === 'Threaded.php') {
$line = debug_backtrace(0)[2]['line'];
$file = debug_backtrace(0)[2]['file'];
}
$this->file = $file; $this->file = $file;
} }
if ($line !== null) { if ($line !== null) {
@ -61,6 +42,7 @@ class Exception extends \Exception
if (strpos($message, 'socket_accept') === false) { if (strpos($message, 'socket_accept') === false) {
\danog\MadelineProto\Logger::log($message.' in '.basename($this->file).':'.$this->line, \danog\MadelineProto\Logger::FATAL_ERROR); \danog\MadelineProto\Logger::log($message.' in '.basename($this->file).':'.$this->line, \danog\MadelineProto\Logger::FATAL_ERROR);
} }
if (in_array($message, ['The session is corrupted!', 'Re-executing query...', 'I had to recreate the temporary authorization key', 'This peer is not present in the internal peer database', "Couldn't get response", 'Chat forbidden', 'The php-libtgvoip extension is required to accept and manage calls. See daniil.it/MadelineProto for more info.', 'File does not exist', 'Please install this fork of phpseclib: https://github.com/danog/phpseclib'])) { if (in_array($message, ['The session is corrupted!', 'Re-executing query...', 'I had to recreate the temporary authorization key', 'This peer is not present in the internal peer database', "Couldn't get response", 'Chat forbidden', 'The php-libtgvoip extension is required to accept and manage calls. See daniil.it/MadelineProto for more info.', 'File does not exist', 'Please install this fork of phpseclib: https://github.com/danog/phpseclib'])) {
return; return;
} }
@ -72,6 +54,22 @@ class Exception extends \Exception
} }
} }
public static function extension(string $extensionName)
{
$additional = 'Try running sudo apt-get install php'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'-'.$extensionName.'.';
if ($extensionName === 'libtgvoip') {
$additional = 'Follow the instructions @ https://voip.madelineproto.xyz to install it.';
} elseif ($extensionName === 'prime') {
$additional = 'Follow the instructions @ https://prime.madelineproto.xyz to install it.';
}
$message = 'MadelineProto requires the '.$extensionName.' extension to run. '.$additional;
if (php_sapi_name() !== 'cli') {
echo $message.'<br>';
}
$file = 'MadelineProto';
$line = 1;
return new self($message, 0, null, $file, $line);
}
/** /**
* ExceptionErrorHandler. * ExceptionErrorHandler.
* *

View File

@ -30,8 +30,6 @@ use danog\MadelineProto\MTProtoTools\UpdatesState;
use danog\MadelineProto\Stream\MTProtoTransport\HttpsStream; use danog\MadelineProto\Stream\MTProtoTransport\HttpsStream;
use danog\MadelineProto\Stream\MTProtoTransport\HttpStream; use danog\MadelineProto\Stream\MTProtoTransport\HttpStream;
use danog\MadelineProto\TL\TLCallback; use danog\MadelineProto\TL\TLCallback;
use function Amp\ByteStream\getStdin;
use function Amp\ByteStream\getInputBufferStream;
/** /**
* Manages all of the mtproto stuff. * Manages all of the mtproto stuff.
@ -162,21 +160,6 @@ class MTProto extends AsyncConstruct implements TLCallback
\danog\MadelineProto\Magic::class_exists(); \danog\MadelineProto\Magic::class_exists();
// Parse settings // Parse settings
$this->parse_settings($settings); $this->parse_settings($settings);
if (!defined('\\phpseclib\\Crypt\\Common\\SymmetricKey::MODE_IGE') || \phpseclib\Crypt\Common\SymmetricKey::MODE_IGE !== 7) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['phpseclib_fork']);
}
if (!extension_loaded('xml')) {
throw new Exception(['extension', 'xml']);
}
if (!extension_loaded('fileinfo')) {
throw new Exception(['extension', 'fileinfo']);
}
if (!extension_loaded('json')) {
throw new Exception(['extension', 'json']);
}
if (!extension_loaded('mbstring')) {
throw new Exception(['extension', 'mbstring']);
}
// Connect to servers // Connect to servers
$this->logger->logger(\danog\MadelineProto\Lang::$current_lang['inst_dc'], Logger::ULTRA_VERBOSE); $this->logger->logger(\danog\MadelineProto\Lang::$current_lang['inst_dc'], Logger::ULTRA_VERBOSE);
if (!($this->channels_state instanceof CombinedUpdatesState)) { if (!($this->channels_state instanceof CombinedUpdatesState)) {
@ -302,7 +285,9 @@ class MTProto extends AsyncConstruct implements TLCallback
public function __wakeup_async($backtrace) public function __wakeup_async($backtrace)
{ {
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
set_exception_handler(['\\danog\\MadelineProto\\Serialization', 'serialize_all']); //set_exception_handler(['\\danog\\MadelineProto\\Serialization', 'serialize_all']);
Magic::class_exists();
$this->setup_logger(); $this->setup_logger();
if (\danog\MadelineProto\Magic::$has_thread && is_object(\Thread::getCurrentThread())) { if (\danog\MadelineProto\Magic::$has_thread && is_object(\Thread::getCurrentThread())) {
return; return;
@ -311,22 +296,6 @@ class MTProto extends AsyncConstruct implements TLCallback
if (isset($this->settings['app_info']['lang_code']) && isset(Lang::$lang[$this->settings['app_info']['lang_code']])) { if (isset($this->settings['app_info']['lang_code']) && isset(Lang::$lang[$this->settings['app_info']['lang_code']])) {
Lang::$current_lang = &Lang::$lang[$this->settings['app_info']['lang_code']]; Lang::$current_lang = &Lang::$lang[$this->settings['app_info']['lang_code']];
} }
if (!defined('\\phpseclib\\Crypt\\AES::MODE_IGE')) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['phpseclib_fork']);
}
if (!extension_loaded('xml')) {
throw new Exception(['extension', 'xml']);
}
if (!extension_loaded('fileinfo')) {
throw new Exception(['extension', 'fileinfo']);
}
if (!extension_loaded('mbstring')) {
throw new Exception(['extension', 'mbstring']);
}
if (!extension_loaded('json')) {
throw new Exception(['extension', 'json']);
}
if (!isset($this->referenceDatabase)) { if (!isset($this->referenceDatabase)) {
$this->referenceDatabase = new ReferenceDatabase($this); $this->referenceDatabase = new ReferenceDatabase($this);
} }
@ -455,9 +424,9 @@ class MTProto extends AsyncConstruct implements TLCallback
} }
/*if (!$this->settings['updates']['handle_old_updates']) { /*if (!$this->settings['updates']['handle_old_updates']) {
$this->channels_state = new CombinedUpdatesState(); $this->channels_state = new CombinedUpdatesState();
$this->msg_ids = []; $this->msg_ids = [];
$this->got_state = false; $this->got_state = false;
}*/ }*/
yield $this->connect_to_all_dcs_async(); yield $this->connect_to_all_dcs_async();
foreach ($this->calls as $id => $controller) { foreach ($this->calls as $id => $controller) {
@ -801,11 +770,11 @@ class MTProto extends AsyncConstruct implements TLCallback
], 'upload' => [ ], 'upload' => [
'allow_automatic_upload' => true, 'allow_automatic_upload' => true,
'part_size' => 512 * 1024, 'part_size' => 512 * 1024,
'parallel_chunks' => 20 'parallel_chunks' => 20,
], 'download' => [ ], 'download' => [
'report_broken_media' => true, 'report_broken_media' => true,
'part_size' => 1024 * 1024, 'part_size' => 1024 * 1024,
'parallel_chunks' => 20 'parallel_chunks' => 20,
], 'pwr' => [ ], 'pwr' => [
'pwr' => false, 'pwr' => false,
// Need info ? // Need info ?
@ -970,7 +939,6 @@ class MTProto extends AsyncConstruct implements TLCallback
$this->dialog_params = ['_' => 'MadelineProto.dialogParams', 'limit' => 0, 'offset_date' => 0, 'offset_id' => 0, 'offset_peer' => ['_' => 'inputPeerEmpty'], 'count' => 0]; $this->dialog_params = ['_' => 'MadelineProto.dialogParams', 'limit' => 0, 'offset_date' => 0, 'offset_id' => 0, 'offset_peer' => ['_' => 'inputPeerEmpty'], 'count' => 0];
$this->full_chats = []; $this->full_chats = [];
} }
public function resetUpdateSystem() public function resetUpdateSystem()
{ {

View File

@ -23,10 +23,10 @@ use Amp\DoH\DoHConfig;
use Amp\DoH\Nameserver; use Amp\DoH\Nameserver;
use Amp\DoH\Rfc8484StubResolver; use Amp\DoH\Rfc8484StubResolver;
use Amp\Loop; use Amp\Loop;
use function Amp\ByteStream\getInputBufferStream;
use function Amp\ByteStream\getStdin;
use function Amp\Dns\resolver; use function Amp\Dns\resolver;
use function Amp\Promise\wait; use function Amp\Promise\wait;
use function Amp\ByteStream\getStdin;
use function Amp\ByteStream\getInputBufferStream;
class Magic class Magic
{ {
@ -68,6 +68,14 @@ class Magic
set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']); set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
//set_exception_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionHandler']); //set_exception_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionHandler']);
if (!self::$inited) { if (!self::$inited) {
if (!defined('\\phpseclib\\Crypt\\Common\\SymmetricKey::MODE_IGE') || \phpseclib\Crypt\Common\SymmetricKey::MODE_IGE !== 7) {
throw new Exception(\danog\MadelineProto\Lang::$current_lang['phpseclib_fork']);
}
foreach (['intl', 'xml', 'fileinfo', 'json', 'mbstring'] as $extension) {
if (!extension_loaded($extension)) {
throw Exception::extension($extension);
}
}
self::$has_thread = class_exists('\\Thread') && method_exists('\\Thread', 'getCurrentThread'); self::$has_thread = class_exists('\\Thread') && method_exists('\\Thread', 'getCurrentThread');
self::$BIG_ENDIAN = pack('L', 1) === pack('N', 1); self::$BIG_ENDIAN = pack('L', 1) === pack('N', 1);
self::$bigint = PHP_INT_SIZE < 8; self::$bigint = PHP_INT_SIZE < 8;
@ -147,14 +155,14 @@ class Magic
Loop::onSignal(SIGINT, static function () { Loop::onSignal(SIGINT, static function () {
getStdin()->unreference(); getStdin()->unreference();
getInputBufferStream()->unreference(); getInputBufferStream()->unreference();
Logger::log('Got sigint', Logger::FATAL_ERROR); Logger::log('Got sigint', Logger::FATAL_ERROR);
die(); die();
}); });
/*Loop::onSignal(SIGTERM, static function () { /*Loop::onSignal(SIGTERM, static function () {
Logger::log('Got sigterm', Logger::FATAL_ERROR); Logger::log('Got sigterm', Logger::FATAL_ERROR);
Loop::stop(); Loop::stop();
die(); die();
});*/ });*/
} }
if (!self::$altervista && !self::$zerowebhost) { if (!self::$altervista && !self::$zerowebhost) {
$DohConfig = new DoHConfig( $DohConfig = new DoHConfig(

View File

@ -536,9 +536,6 @@ trait BotAPI
$arguments['message'] = trim($this->html_fixtags($arguments['message'])); $arguments['message'] = trim($this->html_fixtags($arguments['message']));
$dom = new \DOMDocument(); $dom = new \DOMDocument();
if (!extension_loaded('mbstring')) {
throw new \danog\MadelineProto\Exception(['extension', 'mbstring']);
}
$dom->loadHTML(mb_convert_encoding($arguments['message'], 'HTML-ENTITIES', 'UTF-8')); $dom->loadHTML(mb_convert_encoding($arguments['message'], 'HTML-ENTITIES', 'UTF-8'));
if (!isset($arguments['entities'])) { if (!isset($arguments['entities'])) {
$arguments['entities'] = []; $arguments['entities'] = [];

View File

@ -47,7 +47,7 @@ trait AuthKeyHandler
public function request_call_async($user) public function request_call_async($user)
{ {
if (!class_exists('\\danog\\MadelineProto\\VoIP')) { if (!class_exists('\\danog\\MadelineProto\\VoIP')) {
throw new \danog\MadelineProto\Exception(['extension', 'libtgvoip']); throw \danog\MadelineProto\Exception::extension('libtgvoip');
} }
array_walk($this->calls, function ($controller, $id) { array_walk($this->calls, function ($controller, $id) {
if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) {
@ -124,7 +124,7 @@ trait AuthKeyHandler
public function confirm_call_async($params) public function confirm_call_async($params)
{ {
if (!class_exists('\\danog\\MadelineProto\\VoIP')) { if (!class_exists('\\danog\\MadelineProto\\VoIP')) {
throw new \danog\MadelineProto\Exception(['extension', 'libtgvoip']); throw \danog\MadelineProto\Exception::extension('libtgvoip');
} }
array_walk($this->calls, function ($controller, $id) { array_walk($this->calls, function ($controller, $id) {
if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) {
@ -161,7 +161,7 @@ trait AuthKeyHandler
public function complete_call_async($params) public function complete_call_async($params)
{ {
if (!class_exists('\\danog\\MadelineProto\\VoIP')) { if (!class_exists('\\danog\\MadelineProto\\VoIP')) {
throw new \danog\MadelineProto\Exception(['extension', 'libtgvoip']); throw \danog\MadelineProto\Exception::extension('libtgvoip');
} }
array_walk($this->calls, function ($controller, $id) { array_walk($this->calls, function ($controller, $id) {
if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) {
@ -201,7 +201,7 @@ trait AuthKeyHandler
public function call_status($id) public function call_status($id)
{ {
if (!class_exists('\\danog\\MadelineProto\\VoIP')) { if (!class_exists('\\danog\\MadelineProto\\VoIP')) {
throw new \danog\MadelineProto\Exception(['extension', 'libtgvoip']); throw \danog\MadelineProto\Exception::extension('libtgvoip');
} }
array_walk($this->calls, function ($controller, $id) { array_walk($this->calls, function ($controller, $id) {
if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) {
@ -218,7 +218,7 @@ trait AuthKeyHandler
public function get_call($call) public function get_call($call)
{ {
if (!class_exists('\\danog\\MadelineProto\\VoIP')) { if (!class_exists('\\danog\\MadelineProto\\VoIP')) {
throw new \danog\MadelineProto\Exception(['extension', 'libtgvoip']); throw \danog\MadelineProto\Exception::extension('libtgvoip');
} }
array_walk($this->calls, function ($controller, $id) { array_walk($this->calls, function ($controller, $id) {
if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) { if ($controller->getCallState() === \danog\MadelineProto\VoIP::CALL_STATE_ENDED) {
@ -232,7 +232,7 @@ trait AuthKeyHandler
public function discard_call_async($call, $reason, $rating = [], $need_debug = true) public function discard_call_async($call, $reason, $rating = [], $need_debug = true)
{ {
if (!class_exists('\\danog\\MadelineProto\\VoIP')) { if (!class_exists('\\danog\\MadelineProto\\VoIP')) {
throw new \danog\MadelineProto\Exception(['extension', 'libtgvoip']); throw \danog\MadelineProto\Exception::extension('libtgvoip');
} }
if (!isset($this->calls[$call['id']])) { if (!isset($this->calls[$call['id']])) {
return; return;