Skip to content

Error

Typed, JSON serializable exceptions with stable identifiers, shared across the Raxos monorepo.

Raxos Error provides the small set of primitives that every other Raxos package builds its exceptions on. The abstract Exception class pairs a machine readable error code with a human readable description, accepts an optional previous throwable, and serializes cleanly to JSON so it can be returned directly from an HTTP error response. ExceptionId derives a stable, reproducible numeric identifier from a class or method name, removing the need to hand assign exception codes. A ready to use InvalidArgumentException is included as a minimal, concrete example of the pattern.

Highlights

Explore by category

Quick example

php
<?php
declare(strict_types=1);

namespace App\Error;

use Raxos\Error\Exception;

final class UserNotFoundException extends Exception
{
    public function __construct(string $userId)
    {
        parent::__construct(
            error: 'user_not_found',
            errorDescription: "No user exists with id {$userId}."
        );
    }
}

throw new UserNotFoundException('42');

Because no $code is passed, an ExceptionId is derived automatically from the class name, and the resulting exception serializes to JSON with code, error and error_description keys.

Installation

Install the package with Composer and check the requirements on the installation page.

shell
composer require raxos/error