Skip to content

tomphp/exception-constructor-tools

Repository files navigation

Exception Constructor Tools

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

A simple PHP trait which makes creating static constructors for exceptions nicer.

Installation

$ composer require tomphp/exception-constructor-tools

Usage

Define your exception:

<?php

use TomPHP\ExceptionConstructorTools\ExceptionConstructorTools;

class MyExceptionClass extends \RuntimeException
{
    use ExceptionConstructorTools;

    public static function forEntity($entity)
    {
        return self::create(
            'There was an error with an entity of type %s with value of %s.',
            [
                self::typeToString($entity)
                self::valueToString($entity)
            ]
        );
    }
}

Throw your exception:

if ($errorOccurred) {
    throw MyExceptionClass::forEntity($entity);
}

About

A simple PHP trait which makes creating static constructors for exceptions nicer.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages