holicz/simple-exception
Lightweight base exception for PHP/Laravel with structured context: a safe public message for users, a private debug message for logs, and an HTTP status code. Create your own exceptions by extending BaseException and pass an ExceptionContext.
Simple base exception class providing public and private context inspired by https://github.com/EasyCorp/EasyAdminBundle
composer require holicz/simple-exception
<?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);
}
}
try {
...
} catch (MyException $e) {
// Available methods
$e->getPublicMessage(); // Show to user
$e->getDebugMessage(); // Log
$e->getStatusCode();
}
How can I help you explore Laravel packages today?