From 5f4a00c77e2815f1d877b500fb8d30179bbec290 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Tue, 15 Nov 2022 21:48:46 +0800 Subject: [PATCH 1/6] refactor ZMException --- src/ZM/Bootstrap/HandleExceptions.php | 2 +- src/ZM/Exception/ConfigException.php | 13 ++++--------- src/ZM/Exception/Handler.php | 16 +++++----------- src/ZM/Exception/InitException.php | 7 +++++++ src/ZM/Exception/InterruptException.php | 3 ++- src/ZM/Exception/InvalidArgumentException.php | 3 ++- src/ZM/Exception/ZMException.php | 6 ++++++ 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/ZM/Bootstrap/HandleExceptions.php b/src/ZM/Bootstrap/HandleExceptions.php index 709552d3..0ca4e13e 100644 --- a/src/ZM/Bootstrap/HandleExceptions.php +++ b/src/ZM/Bootstrap/HandleExceptions.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace ZM\Bootstrap; -use OneBot\Driver\ExceptionHandler; +use OneBot\Exception\ExceptionHandler; use ZM\Exception\Handler; class HandleExceptions diff --git a/src/ZM/Exception/ConfigException.php b/src/ZM/Exception/ConfigException.php index 7d415943..6909d703 100644 --- a/src/ZM/Exception/ConfigException.php +++ b/src/ZM/Exception/ConfigException.php @@ -6,22 +6,17 @@ namespace ZM\Exception; class ConfigException extends ZMException { - public const UNSUPPORTED_FILE_TYPE = 'E00079'; + public const UNSUPPORTED_FILE_TYPE = 79; - public const LOAD_CONFIG_FAILED = 'E00080'; - - public function __construct($err_code, $message = '', $code = 0, \Throwable $previous = null) - { - parent::__construct(zm_internal_errcode($err_code) . $message, $code, $previous); - } + public const LOAD_CONFIG_FAILED = 80; public static function unsupportedFileType(string $file_path): ConfigException { - return new self(self::UNSUPPORTED_FILE_TYPE, "不支持的配置文件类型:{$file_path}"); + return new self("不支持的配置文件类型:{$file_path}", "请检查配置文件的后缀名是否正确", self::UNSUPPORTED_FILE_TYPE); } public static function loadConfigFailed(string $file_path, string $message): ConfigException { - return new self(self::LOAD_CONFIG_FAILED, "加载配置文件失败:{$file_path},{$message}"); + return new self("加载配置文件失败:{$file_path},{$message}", "请检查配置文件的格式是否正确,并尝试按照错误信息排查", self::LOAD_CONFIG_FAILED); } } diff --git a/src/ZM/Exception/Handler.php b/src/ZM/Exception/Handler.php index e2457320..24358f56 100644 --- a/src/ZM/Exception/Handler.php +++ b/src/ZM/Exception/Handler.php @@ -4,14 +4,14 @@ declare(strict_types=1); namespace ZM\Exception; -use OneBot\Driver\ExceptionHandler; +use OneBot\Exception\ExceptionHandler; +use OneBot\Exception\ExceptionHandlerInterface; -class Handler extends ExceptionHandler +class Handler extends ExceptionHandler implements ExceptionHandlerInterface { public function __construct() { - // 我们知道此处没有调用父类的构造函数,这是设计上的缺陷 - // 将会在稍后修复 + parent::__construct(); } public function handle(\Throwable $e): void @@ -21,12 +21,6 @@ class Handler extends ExceptionHandler // TODO } - if (is_null($this->whoops)) { - ob_logger()->error('Uncaught ' . get_class($e) . ': ' . $e->getMessage() . ' at ' . $e->getFile() . '(' . $e->getLine() . ')'); - ob_logger()->error($e->getTraceAsString()); - return; - } - -// $this->whoops->handleException($e); + $this->handle0($e); } } diff --git a/src/ZM/Exception/InitException.php b/src/ZM/Exception/InitException.php index 49ba9dce..fed7146c 100644 --- a/src/ZM/Exception/InitException.php +++ b/src/ZM/Exception/InitException.php @@ -4,6 +4,13 @@ declare(strict_types=1); namespace ZM\Exception; +use Throwable; + class InitException extends ZMException { + public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null) + { + // TODO: change this to a better error message + parent::__construct($message, '', $code, $previous); + } } diff --git a/src/ZM/Exception/InterruptException.php b/src/ZM/Exception/InterruptException.php index 3dcb2286..fd8bb65b 100644 --- a/src/ZM/Exception/InterruptException.php +++ b/src/ZM/Exception/InterruptException.php @@ -10,7 +10,8 @@ class InterruptException extends ZMException public function __construct($return_var = null, $message = '', $code = 0, \Throwable $previous = null) { - parent::__construct($message, $code, $previous); + // TODO: change this to a better error message + parent::__construct($message, '', $code, $previous); $this->return_var = $return_var; } } diff --git a/src/ZM/Exception/InvalidArgumentException.php b/src/ZM/Exception/InvalidArgumentException.php index 460b0bf0..babe269f 100644 --- a/src/ZM/Exception/InvalidArgumentException.php +++ b/src/ZM/Exception/InvalidArgumentException.php @@ -8,6 +8,7 @@ class InvalidArgumentException extends ZMException { public function __construct($message = '', $code = 0, \Throwable $previous = null) { - parent::__construct(zm_internal_errcode('E00074') . $message, $code, $previous); + // TODO: change this to a better error message + parent::__construct($message, '', 74, $previous); } } diff --git a/src/ZM/Exception/ZMException.php b/src/ZM/Exception/ZMException.php index e5fef9a8..35e43ac2 100644 --- a/src/ZM/Exception/ZMException.php +++ b/src/ZM/Exception/ZMException.php @@ -4,6 +4,12 @@ declare(strict_types=1); namespace ZM\Exception; +use Throwable; + abstract class ZMException extends \Exception { + public function __construct(string $description, string $solution = '', int $code = 0, ?Throwable $previous = null) + { + parent::__construct($description . PHP_EOL . $solution, $code, $previous); + } } From 352f3e920e51fa84c70a0d836eae46215b3e864a Mon Sep 17 00:00:00 2001 From: sunxyw Date: Tue, 15 Nov 2022 21:51:15 +0800 Subject: [PATCH 2/6] deprecate ZMKnownException --- src/ZM/Exception/ZMKnownException.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/ZM/Exception/ZMKnownException.php b/src/ZM/Exception/ZMKnownException.php index cd176782..28e0d310 100644 --- a/src/ZM/Exception/ZMKnownException.php +++ b/src/ZM/Exception/ZMKnownException.php @@ -4,10 +4,21 @@ declare(strict_types=1); namespace ZM\Exception; +use JetBrains\PhpStorm\Deprecated; + +#[Deprecated(reason: '建议使用具体的异常类')] class ZMKnownException extends ZMException { public function __construct($err_code, $message = '', $code = 0, \Throwable $previous = null) { parent::__construct(zm_internal_errcode($err_code) . $message, $code, $previous); + if ($err_code === 'E99999') { + $code = 0; + // 这也太懒了吧 + } else { + // 取最后两数 + $code = (int)substr($err_code, -2); + } + parent::__construct($message, '', $code, $previous); } } From 5a3da43640bc7c39ccb2b55eda6628eca166f7bf Mon Sep 17 00:00:00 2001 From: sunxyw Date: Tue, 15 Nov 2022 23:11:24 +0800 Subject: [PATCH 3/6] fix unused argument --- src/ZM/Exception/InvalidArgumentException.php | 2 +- src/entry.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ZM/Exception/InvalidArgumentException.php b/src/ZM/Exception/InvalidArgumentException.php index babe269f..355d93e2 100644 --- a/src/ZM/Exception/InvalidArgumentException.php +++ b/src/ZM/Exception/InvalidArgumentException.php @@ -9,6 +9,6 @@ class InvalidArgumentException extends ZMException public function __construct($message = '', $code = 0, \Throwable $previous = null) { // TODO: change this to a better error message - parent::__construct($message, '', 74, $previous); + parent::__construct($message, '', $code ?: 74, $previous); } } diff --git a/src/entry.php b/src/entry.php index 754cf405..622549c8 100644 --- a/src/entry.php +++ b/src/entry.php @@ -2,13 +2,14 @@ declare(strict_types=1); -use OneBot\Driver\ExceptionHandler; - /** * CLI Application 入口文件,先引入 Composer 组件 * * @noinspection PhpIncludeInspection */ + +use OneBot\Exception\ExceptionHandler; + require_once((!is_dir(__DIR__ . '/../vendor')) ? getcwd() : (__DIR__ . '/..')) . '/vendor/autoload.php'; // 适配 Windows 的 conhost 中文显示,因为使用 micro 打包框架运行的时候在 Windows 运行中文部分会变成乱码 From 44c32c171190b7598c531edb5620e715aa389621 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Tue, 15 Nov 2022 23:14:31 +0800 Subject: [PATCH 4/6] fix styles --- src/ZM/Exception/ConfigException.php | 4 ++-- src/ZM/Exception/InitException.php | 4 +--- src/ZM/Exception/ZMException.php | 4 +--- src/ZM/Exception/ZMKnownException.php | 4 ++-- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/ZM/Exception/ConfigException.php b/src/ZM/Exception/ConfigException.php index 6909d703..86233125 100644 --- a/src/ZM/Exception/ConfigException.php +++ b/src/ZM/Exception/ConfigException.php @@ -12,11 +12,11 @@ class ConfigException extends ZMException public static function unsupportedFileType(string $file_path): ConfigException { - return new self("不支持的配置文件类型:{$file_path}", "请检查配置文件的后缀名是否正确", self::UNSUPPORTED_FILE_TYPE); + return new self("不支持的配置文件类型:{$file_path}", '请检查配置文件的后缀名是否正确', self::UNSUPPORTED_FILE_TYPE); } public static function loadConfigFailed(string $file_path, string $message): ConfigException { - return new self("加载配置文件失败:{$file_path},{$message}", "请检查配置文件的格式是否正确,并尝试按照错误信息排查", self::LOAD_CONFIG_FAILED); + return new self("加载配置文件失败:{$file_path},{$message}", '请检查配置文件的格式是否正确,并尝试按照错误信息排查', self::LOAD_CONFIG_FAILED); } } diff --git a/src/ZM/Exception/InitException.php b/src/ZM/Exception/InitException.php index fed7146c..538bf743 100644 --- a/src/ZM/Exception/InitException.php +++ b/src/ZM/Exception/InitException.php @@ -4,11 +4,9 @@ declare(strict_types=1); namespace ZM\Exception; -use Throwable; - class InitException extends ZMException { - public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null) + public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null) { // TODO: change this to a better error message parent::__construct($message, '', $code, $previous); diff --git a/src/ZM/Exception/ZMException.php b/src/ZM/Exception/ZMException.php index 35e43ac2..67abda11 100644 --- a/src/ZM/Exception/ZMException.php +++ b/src/ZM/Exception/ZMException.php @@ -4,11 +4,9 @@ declare(strict_types=1); namespace ZM\Exception; -use Throwable; - abstract class ZMException extends \Exception { - public function __construct(string $description, string $solution = '', int $code = 0, ?Throwable $previous = null) + public function __construct(string $description, string $solution = '', int $code = 0, ?\Throwable $previous = null) { parent::__construct($description . PHP_EOL . $solution, $code, $previous); } diff --git a/src/ZM/Exception/ZMKnownException.php b/src/ZM/Exception/ZMKnownException.php index 28e0d310..c514b9ef 100644 --- a/src/ZM/Exception/ZMKnownException.php +++ b/src/ZM/Exception/ZMKnownException.php @@ -14,10 +14,10 @@ class ZMKnownException extends ZMException parent::__construct(zm_internal_errcode($err_code) . $message, $code, $previous); if ($err_code === 'E99999') { $code = 0; - // 这也太懒了吧 + // 这也太懒了吧 } else { // 取最后两数 - $code = (int)substr($err_code, -2); + $code = (int) substr($err_code, -2); } parent::__construct($message, '', $code, $previous); } From 8199e91ce07b4ebdae42ddd5863fd5bca81c72c0 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Thu, 17 Nov 2022 21:11:11 +0800 Subject: [PATCH 5/6] remove todo from InterruptException --- src/ZM/Exception/InterruptException.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ZM/Exception/InterruptException.php b/src/ZM/Exception/InterruptException.php index fd8bb65b..904404f3 100644 --- a/src/ZM/Exception/InterruptException.php +++ b/src/ZM/Exception/InterruptException.php @@ -10,7 +10,6 @@ class InterruptException extends ZMException public function __construct($return_var = null, $message = '', $code = 0, \Throwable $previous = null) { - // TODO: change this to a better error message parent::__construct($message, '', $code, $previous); $this->return_var = $return_var; } From b5c91531fab2f460f60a281f71087842fe1ae841 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Fri, 18 Nov 2022 15:16:48 +0800 Subject: [PATCH 6/6] fix comment position --- src/entry.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/entry.php b/src/entry.php index 622549c8..0bfbbece 100644 --- a/src/entry.php +++ b/src/entry.php @@ -2,14 +2,9 @@ declare(strict_types=1); -/** - * CLI Application 入口文件,先引入 Composer 组件 - * - * @noinspection PhpIncludeInspection - */ - use OneBot\Exception\ExceptionHandler; +// CLI Application 入口文件,先引入 Composer 组件 require_once((!is_dir(__DIR__ . '/../vendor')) ? getcwd() : (__DIR__ . '/..')) . '/vendor/autoload.php'; // 适配 Windows 的 conhost 中文显示,因为使用 micro 打包框架运行的时候在 Windows 运行中文部分会变成乱码