raxos / error
Error handling related primitives.
Requires
- php: >=8.5
- raxos/contract: *
Requires (Dev)
- pestphp/pest: ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Raxos Error
A common exception base with machine-readable error names, descriptions and numeric IDs.
Documentation | Packagist | Raxos
- Exceptions implement the shared
ExceptionInterfaceand serialize to JSON. ExceptionIdderives repeatable IDs from a class or method name.- A shared
InvalidArgumentExceptionfor invalid inputs.
Installation
Requires PHP 8.5 or later. Composer checks the remaining package and extension dependencies declared in composer.json.
composer require "raxos/error:^3.2"
Usage
<?php declare(strict_types=1); use Raxos\Error\Exception; require __DIR__ . '/vendor/autoload.php'; final class ProductNotFound extends Exception { public function __construct(int $id) { parent::__construct( error: 'product_not_found', errorDescription: "Product {$id} does not exist." ); } } echo json_encode(new ProductNotFound(42));
The JSON representation contains code, error and error_description. If no code is supplied, the base exception derives one from the concrete exception class. A previous exception can be passed through the constructor.
Documentation
Testing
Run this library's Pest suite from the Raxos workspace:
git clone --recurse-submodules https://github.com/basmilius/raxos.git
cd raxos
composer install
vendor/bin/pest --testsuite=error
See Testing Raxos for PHP extensions, integration services and coverage commands. The library's Tests workflow also runs in GitHub Actions.
License
MIT. Copyright (c) 2017 - present Bas Milius.