Exceptions

Basic usage

To create simpler and meaningful exceptions, The VException class renders errors in a homological output with a unique code. So, The devs could use the code to find the issue faster or even for translating the error messages in other platforms. The exceptions arrive in two different form that we will review them in the continue.

Full form

The full form can contain several errors and able us to keep errors of an entity in a same exception class.

 1use Hans\Valravn\Exceptions\VException;
 2use Symfony\Component\HttpFoundation\Response;
 3
 4class FullFormException extends VException
 5{
 6    protected string $errorCodePrefix = 'FFEcx';
 7
 8    public static function failedRemove(string $param): self
 9    {
10        return new self("Failed to remove $param", 1);
11    }
12
13    public static function notFount(): self
14    {
15        return new self('Failed to find your data', 2, Response::HTTP_NOT_FOUND);
16    }
17}

Compact form

The compact form is simpler and contain just one error. The other difference between the Full and Compact forms are the naming. Take a look at the below Compact exception and compare with the Full form.

 1use Hans\Valravn\Exceptions\VException;
 2use Symfony\Component\HttpFoundation\Response;
 3
 4class CompactFormException extends VException
 5{
 6    protected string $errorCodePrefix = 'CFEcx';
 7
 8    public function __construct(string $class)
 9    {
10        parent::__construct("Class '$class' does not exist", 1, Response::HTTP_NOT_FOUND);
11    }
12}

Convert exceptions

To convert all exceptions to Valravn style exceptions, use the using method like below in bootstrap/app.php file:

1->withExceptions(function (Exceptions $exceptions) {
2    $exceptions->render(\Hans\Valravn\Exceptions\VHandler::using());
3});