easyswoole框架控制器类中异常接管处理,只需要在控制器类中重写父类的exception处理方法即可。在调用的方法中使用:throw new \HttpException("出现异常");抛出异常。
easyswoole框架控制器类中异常接管处理,只需要在控制器类中重写父类的exception处理方法即可。
在调用的方法中使用:throw new HttpException("出现异常");抛出异常。
<?php
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\Controller;
class Index extends Controller
{
public function index()
{
\EasySwoole\EasySwoole\Trigger::getInstance()->error('控制器index方法中抛出的异常');
$file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/welcome.html';
if(!is_file($file)){
$file = EASYSWOOLE_ROOT.'/src/Resource/Http/welcome.html';
}
$this->response()->write(file_get_contents($file));
}
function test()
{
$this->response()->write('this is test');
}
protected function actionNotFound(?string $action)
{
$this->response()->withStatus(404);
$file = EASYSWOOLE_ROOT.'/vendor/easyswoole/easyswoole/src/Resource/Http/404.html';
if(!is_file($file)){
$file = EASYSWOOLE_ROOT.'/src/Resource/Http/404.html';
}
$this->response()->write(file_get_contents($file));
}
//重写父类的exception处理方法
protected function onException(\Throwable $throwable): void
{
//拦截错误进日志,使控制器继续运行
\EasySwoole\EasySwoole\Trigger::getInstance()->throwable($throwable);
$this->writeJson(\EasySwoole\Http\Message\Status::CODE_INTERNAL_SERVER_ERROR, null, $throwable->getMessage());
}
}
[2022-01-09 18:03:34][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:35][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:35][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:35][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:35][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:36][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:36][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:36][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:36][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:36][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:37][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:37][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:37][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
[2022-01-09 18:03:37][debug][debug]:[这是自定义输出的错误:控制器index方法中抛出的异常]
转载注明:
感谢博主,喝杯咖啡~
感谢博主,喝杯咖啡~
还没有人发表评论