holicz / simple-exception
Simple base exception class
Installs: 3 341
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.4|^8.0
README
Simple base exception class providing public and private context inspired by https://github.com/EasyCorp/EasyAdminBundle
Installation and usage
Installation with composer
composer require holicz/simple-exception
Your exception class:
<?php namespace App\Exception; use holicz\SimpleException\BaseException; use holicz\SimpleException\ExceptionContext; class CouldNotRemoveArticleException extends BaseException { public function __construct(int $id) { $exceptionContext = new ExceptionContext( 'There was an error during article removal. Please try again later.', sprintf('Could not delete article with id %d', $id), 500 // HTTP status code ); parent::__construct($exceptionContext); } }
Your code
try { ... } catch (MyException $e) { // Available methods $e->getPublicMessage(); // Show to user $e->getDebugMessage(); // Log $e->getStatusCode(); }