artex / exceptions
Artex Software's standard PHP exception library for consistent error handling.
Requires
- php: >=8.0
This package is auto-updated.
Last update: 2025-04-26 19:55:41 UTC
README
ARTEX EXCEPTIONS
Custom PHP Exceptions Library
A simple, consistent library for creating and managing exceptions in PHP projects.
The Artex Standard Exceptions Library provides a basic, flexible way to handle exceptions in your PHP applications. Built to align with good practices, it simplifies exception management and adds a touch of consistency to your projects.
While this library isn't aiming to reinvent the wheel, it serves as a standard tool for PHP projects under the Artex Software ecosystem, ensuring reliability and clarity for developers.
Key Features
- Custom Exceptions: Extendable exceptions for common use cases (e.g., runtime errors, validation errors, database errors).
- PSR-4 Compliant: Easy integration into modern PHP projects.
- Contextual Data: Include optional context information for better debugging.
- Extensibility: Provides a foundation to build project-specific exceptions.
Installation
composer "require artex/exceptions"
Usage
Basic Example
use Artex\Exceptions\ArtexRuntimeException; try { throw new ArtexRuntimeException("Something went wrong!", 500); } catch (ArtexRuntimeException $e) { echo $e->getMessage(); }
Adding Context to Exceptions
use Artex\Exceptions\ArtexException; try { throw new ArtexException( "Validation failed!", 422, ['field' => 'email', 'error' => 'Invalid format'] ); } catch (ArtexException $e) { // Access additional context var_dump($e->getContext()); }
Extending Exceptions
You can easily extend any Artex exception to create project-specific exceptions:
namespace MyApp\Exceptions; use Artex\Exceptions\ArtexRuntimeException; class CustomException extends ArtexRuntimeException { public function __construct(string $message = "", int $code = 0, array $context = []) { parent::__construct($message, $code, $context); } }
Logging Exceptions
The library can seamlessly log exceptions if a PSR-3 compatible logger is available:
use Artex\Exceptions\ArtexRuntimeException; use Monolog\Logger; use Monolog\Handler\StreamHandler; // Set up a PSR-3 logger $logger = new Logger('example'); $logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG)); // Register the logger ArtexRuntimeException::registerLogger($logger); try { throw new ArtexRuntimeException("Critical failure", 500); } catch (ArtexRuntimeException $e) { // Exception is automatically logged echo "Exception logged: " . $e->getMessage(); }
Available Exceptions
The library ships with the following prebuilt exceptions:
Exception Class | Description |
---|---|
ArtexException |
Base exception class for all custom exceptions. |
ArtexRuntimeException |
For runtime errors. |
ArtexInvalidArgumentException |
For invalid arguments passed to functions. |
ArtexLogicException |
For logical errors in the application. |
ArtexUnexpectedValueException |
For unexpected values in operations. |
ValidationException |
For validation-specific errors. |
DatabaseException |
For database-related errors. |
FileException |
For file-related errors. |
Advanced Usage
Extending Exceptions
You can easily extend any Artex exception to create project-specific exceptions:
namespace MyApp\Exceptions; use Artex\Exceptions\ArtexRuntimeException; class CustomException extends ArtexRuntimeException { public function __construct(string $message = "", int $code = 0, array $context = []) { parent::__construct($message, $code, $context); } }
Requirements
- PHP 8.0 or higher
Contributing
We welcome contributions to the Artex Standard Exceptions Library! If you have an idea, feature request, or find a bug, please open an issue or submit a pull request.
LICENSE
This software is distributed under the Apache 2.0 License, granting you the freedom to use, modify, and distribute the software, provided you adhere to the terms outlined in the license.
See the LICENSE file for details.