目录

HTTP基础应用 - 错误与异常拦截

优质
小牛编辑
134浏览
2023-12-01

异常处理

示例:

  1. <?php
  2. namespace EasySwoole;
  3. use EasySwoole\Core\Component\Di;
  4. use EasySwoole\Core\Component\SysConst;
  5. use EasySwoole\Core\Http\Request;
  6. use EasySwoole\Core\Http\Response;
  7. use EasySwoole\Core\Swoole\EventRegister;
  8. use EasySwoole\Core\Swoole\ServerManager;
  9. use EasySwoole\Core\AbstractInterface\EventInterface;
  10. class EasySwooleEvent implements EventInterface
  11. {
  12. public static function frameInitialize() : void
  13. {
  14. Di::getInstance()->set( SysConst::HTTP_EXCEPTION_HANDLER, \App\ExceptionHandler::class );
  15. }
  16. .....
  17. }

\App\ExceptionHandler 文件

  1. <?php
  2. namespace App;
  3. use EasySwoole\Core\Http\AbstractInterface\ExceptionHandlerInterface;
  4. use EasySwoole\Core\Http\Request;
  5. use EasySwoole\Core\Http\Response;
  6. class ExceptionHandler implements ExceptionHandlerInterface
  7. {
  8. public function handle( \Throwable $exception, Request $request, Response $response )
  9. {
  10. var_dump($exception);
  11. }
  12. }

试试在控制器里,随便实例化一个不存在的类,如:new B() 让其报错下试试。

注意:长连接时请做好异常处理,因为在mysql断开时会抛出错误中断运行,这时捕获下重新连接再执行sql语句。

相关资源

Whoops to easySwoole framework 2.x https://github.com/easy-swoole/easyWhoops