hradigital/php-exceptions
Composer 安装命令:
composer require hradigital/php-exceptions
包简介
PHP Domain Exceptions library — a curated set of reusable, semantic exceptions for Domain-Driven Design applications.
README 文档
README
A curated set of reusable, semantic domain exceptions for Domain-Driven Design (DDD) PHP applications.
This package is framework-agnostic — zero runtime dependencies beyond the PHP standard library. It was extracted from hradigital/php-datatypes so it can evolve independently and be reused across any PHP 8.1+ project.
Why this package?
These are domain exceptions, not HTTP exceptions. Their names and intent mirror the HTTP 4XX/5XX status families because the vocabulary is widely understood — but they are meant to be raised in the domain layer, outside any transport, and translated to a transport-specific representation (HTTP response, RPC error, CLI exit, queue dead-letter) at the boundary.
Throwing the right exception is part of your domain language. Generic \InvalidArgumentException or \RuntimeException leak plumbing concerns into the domain. This library gives you:
- Semantic, intent-revealing exception types organised into Client (caller-fault, 4XX-aligned) and Server (system-fault, 5XX-aligned) families.
- A common abstract base (
AbstractBaseException) that extends\DomainExceptionso a singlecatchcan capture everything in this package. - No framework lock-in — pure PHP, drop it into any application or framework.
Requirements
| Dependency | Version |
|---|---|
| PHP | ^8.1 |
Installation
composer require hradigital/php-exceptions
Usage
Every exception in this package extends AbstractBaseException, which itself extends PHP's native \DomainException. Each subclass ships with a sensible default message and an HTTP-aligned status code ($code).
use HraDigital\Components\Exceptions\Client\NotFoundException; throw new NotFoundException('User #42 does not exist.');
Catching by intent
use HraDigital\Components\Exceptions\AbstractBaseException; use HraDigital\Components\Exceptions\Client\ConflictException; use HraDigital\Components\Exceptions\Client\NotFoundException; use HraDigital\Components\Exceptions\Client\UnprocessableEntityException; try { $service->updateProfile($payload); } catch (UnprocessableEntityException $e) { // input parsed but failed business-rule validation } catch (NotFoundException $e) { // target aggregate / entity does not exist } catch (ConflictException $e) { // current state of the aggregate refuses this action } catch (AbstractBaseException $e) { // any other domain failure raised by this package }
Authoring your own exceptions
Pick the closest leaf class and extend it — that way callers can still catch by the broader intent.
use HraDigital\Components\Exceptions\Client\ConflictException; class EmailAlreadyTakenException extends ConflictException { protected $message = 'The provided email is already in use.'; }
Available exceptions
Listed in HTTP-status order for orientation. Each class carries a docBlock with its semantic meaning, when to extend it, and (where relevant) the browser behaviour the analogous HTTP status triggers.
Client — caller-fault (4XX-aligned)
BadRequestException (400), DeniedAccessException (401), PaymentRequiredException (402), ForbiddenException (403), NotFoundException (404), MethodNotAllowedException (405), NotAcceptableException (406), RequestTimeoutException (408), ConflictException (409), GoneException (410), PreconditionFailedException (412), UnsupportedMediaTypeException (415), RequestedRangeNotSatisfiableException (416), ExpectationFailedException (417), UnprocessableEntityException (422), LockedException (423), FailedDependencyException (424), TooEarlyException (425), PreconditionRequiredException (428), TooManyRequestsException (429), UnavailableForLegalReasonsException (451), plus Request\RequestFailureException for structured per-field validation failures.
Server — system-fault (5XX-aligned)
InternalServerErrorException (500), ServerNotImplementedException (501), BadGatewayException (502), ServerUnavailableException (503), GatewayTimeoutException (504), InsufficientStorageException (507), LoopDetectedException (508).
Transport-only HTTP statuses (407, 411, 413, 414, 421, 426, 431, 505, 506, 510, 511) are intentionally not represented — they have no platform-agnostic domain meaning.
Testing
composer install
composer test
Continuous Integration
GitHub Actions runs PHPUnit on every push and pull request, across PHP 8.1 / 8.2 / 8.3 / 8.4.
Versioning
This package follows Semantic Versioning 2.0.0. Breaking changes only ship in major releases.
Contributing
Pull requests are welcome. For substantial changes, please open an issue first to discuss what you'd like to change. Add tests for any new behaviour or bug fix.
Security
If you discover a security vulnerability, please email github@hradigital.com instead of opening a public issue.
License
This package is open-sourced software licensed under the GNU General Public License v3.0 or later.
hradigital/php-exceptions 适用场景与选型建议
hradigital/php-exceptions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「exceptions」 「domain」 「ddd」 「domain-driven-design」 「domain-exceptions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hradigital/php-exceptions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hradigital/php-exceptions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hradigital/php-exceptions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
php7ify is a project that brings new php7 classes and exceptions to php 5.x.
The CodenamePHP Platform core that contains basic interfaces, exceptions etc..
A simple package to better handle Exceptions.
Build a domain-oriented application on Laravel Framework
Command bus implementation: Commands and domain events
A module manager for Zend Framework which can be used to create configs per domain.
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 39
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2026-05-05