easyswoole异常接管处理

Swoole / 1335人浏览 / 0人评论

easyswoole框架控制器类中异常接管处理,只需要在控制器类中重写父类的exception处理方法即可。在调用的方法中使用:throw new \HttpException("出现异常");抛出异常。

easyswoole异常接管

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方法中抛出的异常]

转载注明:

0 条评论

还没有人发表评论

发表评论 取消回复

记住我的信息,方便下次评论
有人回复时邮件通知我