maatify/exceptions 问题修复 & 功能扩展

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

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

maatify/exceptions

Composer 安装命令:

composer require maatify/exceptions

包简介

Policy-driven, escalation-aware exception library for PHP applications.

README 文档

README

Latest Version PHP Version License

PHPStan

Changelog Security

Monthly Downloads Total Downloads

Enterprise-grade, hardened exception handling library for PHP applications.

maatify/exceptions provides a strictly typed, immutable taxonomy for application errors, enforcing semantic correctness, security, and consistent HTTP mapping. It is designed to prevent "taxonomy drift" and ensure that critical system errors are never masked by lower-severity wrappers.

🚀 Key Features

  • Strict Taxonomy: Exceptions are categorized into 9 distinct families (System, Validation, Auth, etc.) backed by ErrorCategoryEnum.
  • Guarded Overrides: Prevents developers from accidentally mismatching error codes or HTTP statuses.
  • Escalation Protection: Automatically escalates severity when a critical exception is wrapped in a lighter one (e.g., a Database failure wrapped in a generic runtime exception will typically retain 500 status).
  • Error Serialization: Transforms any Throwable into a standardized, deterministic JSON response suitable for APIs.
  • RFC7807 Support: Built-in formatter for "Problem Details for HTTP APIs" compliance.
  • Zero Dependencies: Pure PHP implementation. No framework coupling.
  • PSR-4 Compliant: Ready for immediate Composer autoloading.

Requirements

  • PHP 8.2+

📦 Installation

composer require maatify/exceptions

📖 Usage

Basic Usage

Throwing a predefined exception:

use Maatify\Exceptions\Exception\Validation\InvalidArgumentMaatifyException;

throw new InvalidArgumentMaatifyException('The email format is invalid.');
// Result:
// Category: VALIDATION
// HTTP Status: 400
// Error Code: INVALID_ARGUMENT

Advanced Usage (Wrapping)

When catching a low-level error and re-throwing, the library automatically handles severity escalation:

use Maatify\Exceptions\Exception\BusinessRule\BusinessRuleMaatifyException;
use Maatify\Exceptions\Exception\System\DatabaseConnectionMaatifyException;

try {
    // Simulate a critical database failure (System / 503)
    throw new DatabaseConnectionMaatifyException('Connection timeout');
} catch (DatabaseConnectionMaatifyException $e) {
    // Attempting to wrap it in a "softer" business exception
    // Note: BusinessRuleMaatifyException is abstract, so we use an anonymous class for this example
    throw new class('Unable to process order', 0, $e) extends BusinessRuleMaatifyException {};
}

// RESULT:
// The final exception will report:
// Category: SYSTEM (Escalated from BusinessRule)
// HTTP Status: 503 (Escalated from 422)
// This ensures monitoring tools see the root cause (System Failure), not a generic Business Rule error.

Error Serialization

Convert any exception into a ready-to-send API response:

use Maatify\Exceptions\Application\Error\ErrorSerializer;
use Maatify\Exceptions\Application\Error\DefaultThrowableToError;
use Maatify\Exceptions\Application\Format\JsonErrorFormatter;

$serializer = new ErrorSerializer(
    new DefaultThrowableToError(),
    new JsonErrorFormatter()
);

try {
    // ... application logic
} catch (\Throwable $t) {
    $response = $serializer->serialize($t);
    
    // Send to client
    http_response_code($response->getStatus());
    header('Content-Type: ' . $response->getContentType());
    echo json_encode($response->getBody());
}

📚 Documentation

Detailed documentation is available in the BOOK/ directory:

  1. Introduction
  2. Architecture
  3. Taxonomy
  4. Exception Families
  5. Override Rules
  6. Escalation Protection
  7. Security Model
  8. Best Practices
  9. Extending The Library
  10. API Integration Guide
  11. Testing Strategy
  12. Versioning Policy
  13. Packagist Metadata

🛡️ Guarantees

  • Category Immutability: An exception's category is defined by its class and cannot be overridden at runtime.
  • Status Class Safety: You cannot force a 4xx exception to return a 5xx status code manually, or vice versa.
  • Escalation Determinism: Severity calculation is deterministic and side-effect free.
  • Serialization Determinism: The same input Throwable and Context will always yield the exact same output.

✅ Quality Status

  • PHP 8.2+
  • PHPUnit 11
  • 100% Code Coverage
  • Zero Warnings
  • Immutable Exception Design
  • Deterministic Escalation & Policy Engine

🪪 License

This library is licensed under the MIT License.
See the LICENSE file for details.

👤 Author

Engineered by Mohamed Abdulalim (@megyptm)
Backend Lead & Technical Architect
https://www.maatify.dev

🤝 Contributors

Special thanks to the Maatify.dev engineering team and all open-source contributors.
Contributions are welcome.

Built with ❤️ by Maatify.dev — Unified Ecosystem for Modern PHP Libraries

maatify/exceptions 适用场景与选型建议

maatify/exceptions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.18k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 maatify/exceptions 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.18k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-27