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');
    }
}
相关推荐
桌面运维家9 小时前
VLAN配置进阶:抑制广播风暴,提升网络效率
开发语言·网络·php
luanma15098017 小时前
Laravel 4.x:现代PHP框架的奠基之作
开发语言·php·laravel
Sgf22718 小时前
第15章 网络编程
开发语言·网络·php
一只会跑会跳会发疯的猴子19 小时前
php操作ssl,亲测可用
android·php·ssl
luanma15098020 小时前
Laravel 5.x核心特性与升级指南
php·laravel
luanma15098020 小时前
Laravel 2.X:现代框架的基石
php·laravel
luanma15098021 小时前
Laravel 6.X重磅升级:7大特性深度解析
php·laravel
m0_738120721 天前
渗透测试——Ripper靶机详细横向渗透过程(rips扫描文件,水平横向越权,Webmin直接获取root权限)
linux·网络·数据库·安全·web安全·php
困死,根本不会1 天前
Kivy+Buildozer 打包 APK 踩坑:python-for-android 克隆失败
开发语言·php·kivy
wefly20171 天前
免安装!m3u8live.cn在线 M3U8 播放器,小白也能快速上手
java·开发语言·python·json·php·m3u8·m3u8在线转换