10. 异常处理器

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

php 复制代码
<?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 复制代码
<?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 复制代码
<?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 复制代码
<?php

namespace App\Exception;

class FooException extends \RuntimeException
{

}

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

php 复制代码
<?php

namespace App\Controller;

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

#[AutoController]
class TestController
{
    public function exception()
    {
        throw new FooException('test');
    }
}
相关推荐
vortex54 小时前
浅谈 Linux 防火墙:从原理到实践
linux·网络·php
come112345 小时前
Claude 写 PHP 项目的完整小白教程
开发语言·php
Bruce_Liuxiaowei11 小时前
PHP文件读取漏洞全面剖析:触发点与利用技术
开发语言·php
hopetomorrow12 小时前
学习路之PHP--webman安装及使用
android·学习·php
链上Sniper1 天前
Python 区块链开发实战:从零到一构建智能合约
开发语言·网络·python·架构·区块链·php·智能合约
链上Sniper1 天前
智能合约安全漏洞解析:从 Reentrancy 到 Integer Overflow
开发语言·网络·架构·区块链·php·智能合约
链上Sniper1 天前
高并发区块链系统实战:从架构设计到性能优化
开发语言·网络·python·性能优化·架构·区块链·php
链上Sniper1 天前
NFT 市场开发:基于 Ethereum 和 IPFS 构建去中心化平台
开发语言·网络·架构·去中心化·区块链·php
链上Sniper1 天前
区块链跨链通信:使用 Cosmos SDK 实现链间互操作
开发语言·网络·架构·区块链·php
时之彼岸Φ1 天前
网络攻防技术十四:入侵检测与网络欺骗
开发语言·网络·php