Merge pull request #173 from zhamao-robot/advance-exception

改进异常
This commit is contained in:
sunxyw
2022-11-18 15:21:33 +08:00
committed by GitHub
9 changed files with 35 additions and 29 deletions

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace ZM\Bootstrap; namespace ZM\Bootstrap;
use OneBot\Driver\ExceptionHandler; use OneBot\Exception\ExceptionHandler;
use ZM\Exception\Handler; use ZM\Exception\Handler;
class HandleExceptions class HandleExceptions

View File

@@ -6,22 +6,17 @@ namespace ZM\Exception;
class ConfigException extends ZMException class ConfigException extends ZMException
{ {
public const UNSUPPORTED_FILE_TYPE = 'E00079'; public const UNSUPPORTED_FILE_TYPE = 79;
public const LOAD_CONFIG_FAILED = 'E00080'; public const LOAD_CONFIG_FAILED = 80;
public function __construct($err_code, $message = '', $code = 0, \Throwable $previous = null)
{
parent::__construct(zm_internal_errcode($err_code) . $message, $code, $previous);
}
public static function unsupportedFileType(string $file_path): ConfigException 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 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);
} }
} }

View File

@@ -4,14 +4,14 @@ declare(strict_types=1);
namespace ZM\Exception; 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() public function __construct()
{ {
// 我们知道此处没有调用父类的构造函数,这是设计上的缺陷 parent::__construct();
// 将会在稍后修复
} }
public function handle(\Throwable $e): void public function handle(\Throwable $e): void
@@ -21,12 +21,6 @@ class Handler extends ExceptionHandler
// TODO // TODO
} }
if (is_null($this->whoops)) { $this->handle0($e);
ob_logger()->error('Uncaught ' . get_class($e) . ': ' . $e->getMessage() . ' at ' . $e->getFile() . '(' . $e->getLine() . ')');
ob_logger()->error($e->getTraceAsString());
return;
}
// $this->whoops->handleException($e);
} }
} }

View File

@@ -6,4 +6,9 @@ namespace ZM\Exception;
class InitException extends ZMException 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);
}
} }

View File

@@ -10,7 +10,7 @@ class InterruptException extends ZMException
public function __construct($return_var = null, $message = '', $code = 0, \Throwable $previous = null) public function __construct($return_var = null, $message = '', $code = 0, \Throwable $previous = null)
{ {
parent::__construct($message, $code, $previous); parent::__construct($message, '', $code, $previous);
$this->return_var = $return_var; $this->return_var = $return_var;
} }
} }

View File

@@ -8,6 +8,7 @@ class InvalidArgumentException extends ZMException
{ {
public function __construct($message = '', $code = 0, \Throwable $previous = null) 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, '', $code ?: 74, $previous);
} }
} }

View File

@@ -6,4 +6,8 @@ namespace ZM\Exception;
abstract class ZMException extends \Exception 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);
}
} }

View File

@@ -4,10 +4,21 @@ declare(strict_types=1);
namespace ZM\Exception; namespace ZM\Exception;
use JetBrains\PhpStorm\Deprecated;
#[Deprecated(reason: '建议使用具体的异常类')]
class ZMKnownException extends ZMException class ZMKnownException extends ZMException
{ {
public function __construct($err_code, $message = '', $code = 0, \Throwable $previous = null) public function __construct($err_code, $message = '', $code = 0, \Throwable $previous = null)
{ {
parent::__construct(zm_internal_errcode($err_code) . $message, $code, $previous); 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);
} }
} }

View File

@@ -2,13 +2,9 @@
declare(strict_types=1); declare(strict_types=1);
use OneBot\Driver\ExceptionHandler; use OneBot\Exception\ExceptionHandler;
/** // CLI Application 入口文件,先引入 Composer 组件
* CLI Application 入口文件,先引入 Composer 组件
*
* @noinspection PhpIncludeInspection
*/
require_once((!is_dir(__DIR__ . '/../vendor')) ? getcwd() : (__DIR__ . '/..')) . '/vendor/autoload.php'; require_once((!is_dir(__DIR__ . '/../vendor')) ? getcwd() : (__DIR__ . '/..')) . '/vendor/autoload.php';
// 适配 Windows 的 conhost 中文显示,因为使用 micro 打包框架运行的时候在 Windows 运行中文部分会变成乱码 // 适配 Windows 的 conhost 中文显示,因为使用 micro 打包框架运行的时候在 Windows 运行中文部分会变成乱码