holicz/simple-exception

Simple base exception class

v1.1.2 2020-02-04 16:05 UTC

This package is auto-updated.

Last update: 2020-11-04 17:49:05 UTC


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 MyException extends BaseException
{
    public function __construct(array $parameters = [])
    {
        $exceptionContext = new ExceptionContext(
            'My public message',
            sprintf('My private message with params: %s', $parameters['key']),
            $parameters,
            500 // HTTP status code
        );
    
        parent::__construct($exceptionContext);
    }
}

Your code

try {
    ...
} catch (MyException $e) {
    // Available methods
    $e->getPublicMessage();
    $e->getDebugMessage();
    $e->getParameters();
    $e->getStatusCode();
}