当前位置: 首页 > article >正文

10. 异常处理器

一、通过 注解 注册异常处理器

<?php
namespace App\Exception\Handler;

use App\Exception\FooException;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Swow\Psr7\Message\ResponsePlusInterface;
use Throwable;

use Hyperf\ExceptionHandler\Annotation\ExceptionHandler as RegisterHandler;

// Hyperf\ExceptionHandler\Annotation\ExceptionHandler 注解 取别名 RegisterHandler
#[RegisterHandler(server: 'http')]
class FooExceptionHandler extends ExceptionHandler
{
    public function handle(Throwable $throwable, ResponsePlusInterface $response)
    {
        echo '异常被执行了';
        return $response->withStatus(501)->withBody(new SwooleStream('This is FooExceptionHandler'));
    }

    public function isValid(Throwable $throwable): bool
    {
        // isValid为true时,执行 handle方法
        return $throwable instanceof FooException;
    }
}

二、通过配置文件注册异常处理器

定义配置文件

  • config/autoload/exception.php
<?php

return [
    'handler' => [
        'http' => [
            App\Exception\Handler\FooExceptionHandler::class,	// 新增配置项
            Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
            App\Exception\Handler\AppExceptionHandler::class,
        ],
    ],
];

定义异常处理器

  • app/Exception/Handler/FooExceptionHandler
<?php
namespace App\Exception\Handler;

use App\Exception\FooException;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Swow\Psr7\Message\ResponsePlusInterface;
use Throwable;

class FooExceptionHandler extends ExceptionHandler
{
    public function handle(Throwable $throwable, ResponsePlusInterface $response)
    {
        $this->stopPropagation();	// 调用该方法,异常不再往后传递
        echo '异常被执行了';
        return $response->withStatus(501)->withBody(new SwooleStream('This is FooExceptionHandler'));
    }

    public function isValid(Throwable $throwable): bool
    {
        // isValid为true时,执行 handle方法
        return $throwable instanceof FooException;
    }
}

定义异常类

  • app/Exception/FooException
<?php

namespace App\Exception;

class FooException extends \RuntimeException
{

}

三、触发异常(调用控制器方法)

<?php

namespace App\Controller;

use App\Exception\FooException;
use Hyperf\HttpServer\Annotation\AutoController;

#[AutoController]
class TestController
{
    public function exception()
    {
        throw new FooException('test');
    }
}

http://www.kler.cn/news/362350.html

相关文章:

  • 2024软件测试面试秘籍(含答案+文档)
  • unity学习-全局光照(GI)
  • 【WPF】中Binding的应用
  • 用户之认证
  • MATLAB图像重心计算
  • 【分布式微服务云原生】《微服务架构下的服务治理探秘》
  • 【某农业大学计算机网络实验报告】实验二 交换机的自学习算法
  • Python小程序 - 替换文件内容
  • Redis Search系列 - 第四讲 支持中文
  • 003:Context Capture10.16安装教程
  • Linux中如何理解一切皆文件
  • 【YOLO模型】(1)--YOLO是什么
  • Android 13 SPRD 如何临时修改 Android 系统版本
  • 开源模型应用落地-Qwen2.5-7B-Instruct与vllm实现离线推理-Tools助力(二)
  • w~自动驾驶合集9
  • RHCE笔记-SSH服务
  • 毕业设计项目系统:基于Springboot框架的网上订餐管理系统,完整源代码+数据库+毕设文档+部署说明
  • Spring Cloud --- Sentinel 授权规则
  • StarTowerChain:开启去中心化创新篇章
  • gitlab-cli无法构建流水线
  • 数据结构 - 树,初探
  • 最好的ppt模板网站是哪个?做PPT不可错过的18个网站!
  • 记录一个容易混淆的 Spring Boot 项目配置文件问题
  • 监控易-某信息化系统监控-监测点详情解读
  • Java学习教程,从入门到精通,Java 基本数据类型(7)
  • 【VUE】v-show 和 v-if 的区别