承接 phrity/util-errorhandler 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

phrity/util-errorhandler

Composer 安装命令:

composer require phrity/util-errorhandler

包简介

Inline error handler; catch and resolve errors for code block.

关键字:

README 文档

README

Phrity Util ErrorHandler

Build Status Coverage Status

Error Handler utility

The PHP error handling can be somewhat of a headache. Typically an application uses a system level error handler and/or suppressing errors using the @ prefix. But those cases when your code need to act on triggered errors are more tricky.

This library provides two convenience methods to handle errors on code blocks, either by throwing exceptions or running callback code when an error occurs.

Installation

Install with Composer;

composer require phrity/util-errorhandler

The Error Handler

The class provides two main methods; with() and withAll(). The difference is that with() will act immediately on an error and abort further code execution, while withAll() will attempt to execute the entire code block before acting on errors that occurred.

Throwing ErrorException

use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$result = $handler->with(function () {
    // Code to execute
    return $success_result;
});
$result = $handler->withAll(function () {
    // Code to execute
    return $success_result;
});

The examples above will run the callback code, but if an error occurs it will throw an ErrorException. Error message and severity will be that of the triggering error.

  • with() will throw immediately when occured
  • withAll() will throw when code is complete; if more than one error occurred, the first will be thrown

Throwing specified Throwable

use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$result = $handler->with(function () {
    // Code to execute
    return $success_result;
}, new RuntimeException('A specified error'));
$result = $handler->withAll(function () {
    // Code to execute
    return $success_result;
}, new RuntimeException('A specified error'));

The examples above will run the callback code, but if an error occurs it will throw provided Throwable. The thrown Throwable will have an ErrorException attached as $previous.

  • with() will throw immediately when occured
  • withAll() will throw when code is complete; if more than one error occurred, the first will be thrown

Using callback

use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$result = $handler->with(function () {
    // Code to execute
    return $success_result;
}, function (ErrorException $error) {
    // Code to handle error
    return $error_result;
});
$result = $handler->withAll(function () {
    // Code to execute
    return $success_result;
}, function (array $errors, $success_result) {
    // Code to handle errors
    return $error_result;
});

The examples above will run the callback code, but if an error occurs it will call the error callback as well.

  • with() will run the error callback immediately when error occured; error callback expects an ErrorException instance
  • withAll() will run the error callback when code is complete; error callback expects an array of ErrorException and the returned result of code callback

Filtering error types

Both with() and withAll() accepts error level(s) as last parameter.

use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$result = $handler->with(function () {
    // Code to execute
    return $success_result;
}, null, E_USER_ERROR);
$result = $handler->withAll(function () {
    // Code to execute
    return $success_result;
}, null, E_USER_ERROR & E_USER_WARNING);

Any value or combination of values accepted by set_error_handler is usable. Default is E_ALL. List of constants.

The global error handler

The class also has global set() and restore() methods.

use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$handler->set(); // Throws ErrorException on error
$handler->set(new RuntimeException('A specified error')); // Throws provided Throwable on error
$handler->set(function (ErrorException $error) {
    // Code to handle errors
    return $error_result;
}); // Runs callback on error
$handler->restore(); // Restores error handler

Class synopsis

Phrity\Util\ErrorHandler {

    /* Methods */
    public __construct()

    public with(callable $callback, mixed $handling = null, int $levels = E_ALL) : mixed
    public withAll(callable $callback, mixed $handling = null, int $levels = E_ALL) : mixed
    public set($handling = null, int $levels = E_ALL) : mixed
    public restore() : bool
}

Versions

Version PHP
1.2 ^8.1 Some improvements
1.1 ^7.4|^8.0 Some improvements
1.0 ^7.2|^8.0 Initial version

phrity/util-errorhandler 适用场景与选型建议

phrity/util-errorhandler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.29M 次下载、GitHub Stars 达 9, 最近一次更新时间为 2021 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 phrity/util-errorhandler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 11.29M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 26
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-09