liuchang103/hyperf-exception 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

liuchang103/hyperf-exception

Composer 安装命令:

composer require liuchang103/hyperf-exception

包简介

hyperf exception api handler

关键字:

README 文档

README

Hyperf 的异常错误处理,有 Json 和 View 自定义渲染

安装

引入包

composer require liuchang103/hyperf-exception

使用

直接抛出 Json

throw new \HyperfException\Json('Error', -1);

// 输出
{"code":-1,"message":"Error"}

直接抛出 View 视图

throw new \HyperfException\View('Error', '404');

// 将抛出 404 的视图文件

进阶

继承 Json

使用常量约定 code 码,让控制器中消失 “数字”

namespace App\Exception;

class Success extends \HyperfException\Json
{
    const CODE = 100;
}

// 抛出
throw new \App\Exception\Success('Created Success');

// 输出
{"code":100,"message":"Created Success"}

定义 Http 状态码

class Error extends \HyperfException\Json
{
    const CODE = 200;
    const HTTP_STATUS = 500;
}

// 抛出
throw new \App\Exception\Error('Created Error');

// 将抛出 500 的 Http 状态
{"code":200,"message":"Created Error"}

定义 json 格式

class Error extends \HyperfException\Json
{
    const CODE = -1;
    const HTTP_STATUS = 500;

    // 自定义 json 格式
    protected function json()
    {
        return [
            'errcode' => $this->getCode(),
            'errmessage' => $this->getMessage()
        ];
    }
}

// 抛出
throw new \App\Exception\Error('Created Error');

// 将抛出 500 的 json 格式
{"errcode":-1,"errmessage":"Created Error"}

自定义传参

class Error extends \HyperfException\Json
{
    const CODE = -1;
    const HTTP_STATUS = 500;

    public function __construct($message, $model)
    {
        parent::__construct($message);

        $this->model = $model;
    }

    // 自定义 json 格式
    protected function json()
    {
        return [
            'errcode' => $this->getCode(),
            'errmessage' => $this->getMessage(),
            'model' => $this->model
        ];
    }
}

// 抛出
throw new \App\Exception\Error('Update Error', $model);

// 将抛出 500 的 json 格式
{"errcode":-1,"errmessage":"Update Error", "model":{}}

继承 View

使用常量约定 VIEW,让控制器中消失 “模版名”

namespace App\Exception;

class Miss extends \HyperfException\View
{
    const VIEW = '404';
}

// 抛出
throw new \App\Exception\Miss('找不到存在的页面');

// 视图文件 404.blade.php
<h1> {{ $message }} </h1>

定义 Http 状态码

class Miss extends \HyperfException\View
{
    const VIEW = '404';
    const HTTP_STATUS = 404;
}

// 抛出
throw new \App\Exception\Miss('找不到存在的页面');

自定义视图传参

class Miss extends \HyperfException\View
{
    const VIEW = '404';
    const HTTP_STATUS = 404;

    protected $model;

    public function __construct($message, $model)
    {
        parent::__construct($message);

        $this->model = $model;
    }

    // 自定义视图参数
    protected function view()
    {
        return [
            'model' => $this->model
        ];
    }
}

// 抛出
throw new \App\Exception\Miss('Error', $model);

// 404.blade.php
{{ $model->name }} 访问页面错误

自定义处理

如果需要自定义渲染,可继承 \HyperfException\Exception 并实现 render 方法,会接收 response 的对象

use HyperfException\Exception;
use Hyperf\HttpMessage\Stream\SwooleStream;

class Message extends Exception
{
    const HTTP_STATUS = 202;
    
    // 自定义渲染
    public function render($response)
    {
        return $response->withBody(new SwooleStream($this->getMessage() . $this->getCode()));
    }
}

// 抛出
throw new \App\Exception\Message('自定义渲染', 1);

// 输出 202 的状态页面
自定义渲染1

HTTP_STATUS 状态码是在更深一层的自定义处理中.

如果要自定义处理,可实现 handle 方法,同样会接收 response 的对象

(注意:实现 handle 方法后,HTTP_STATUS 常量将无效,render 渲染方法也将无效)

public function handle($response)
{
    return $response->withBody(new SwooleStream($this->getMessage() . $this->getCode()));
}

// 输出 200 正常状态的页面
自定义渲染1

liuchang103/hyperf-exception 适用场景与选型建议

liuchang103/hyperf-exception 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.46k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「hyperf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 liuchang103/hyperf-exception 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 liuchang103/hyperf-exception 我们能提供哪些服务?
定制开发 / 二次开发

基于 liuchang103/hyperf-exception 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.46k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-30