cse/base-exceptions

This library provides a simple work with exceptions for both development and production.

1.0.0 2019-05-02 03:13 UTC

This package is auto-updated.

Last update: 2024-11-19 05:23:56 UTC


README

English | Русский

EXCEPTIONS CSE BASE

Travis (.org) Codecov Scrutinizer code quality

Packagist Minimum PHP Version Packagist GitHub repo size

This library provides a simple work with exceptions for both development and production.

Project repository: https://github.com/cs-eliseev/base-exceptions

DEMO

try {
    ..
    $code = 0;
} catch (CseExceptions $e) {
    ...
} catch (Throwable $e) {
    $code = $e->getCode();
}
CseExceptions::getErrorMsg($code);
// Unknown error

Introduction

CSE BASE is a set of libraries written in PHP specifically to extend your classes.

A set of basic methods for creating your classes is what you need to quickly create web applications. EXCEPTIONS CSE BASE, allows you to extend classes with exceptions.

CSE BASE was created for the rapid development of web applications.

CSE Base project:

Below you will find some information on how to init library and perform common commands.

Install

You can find the most recent version of this project here.

Composer

Execute the following command to get the latest version of the package:

composer require cse/base-exceptions

Or file composer.json should include the following contents:

{
    "require": {
        "cse/base-exceptions": "*"
    }
}

Git

Clone this repository locally:

git clone https://github.com/cs-eliseev/base-exceptions.git

Download

Download the latest release here.

Usage

View test model: ModelExceptions.php

See examples: examples-exceptions.php

Create Model Exception

Examples:

class ModelExceptions extends CseExceptions
{
    const ERROR_EXAMPLE_CODE_1 = 1;
    const ERROR_EXAMPLE_CODE_2 = 2;
    const ERROR_EXAMPLE_CODE_3 = 3;

    /**
     * @var array
     */
    protected static $errorsMsg = [
        self::ERROR_EXAMPLE_CODE_1 => 'Error code 1',
        self::ERROR_EXAMPLE_CODE_2 => 'Error code 2',
        self::ERROR_EXAMPLE_CODE_3 => 'Error code 3',
    ];
}

Use check instance

Examples:

try {
    ...
    throw new ModelExceptions('CseExceptions');
} catch (CseExceptions $e) {
    $e->getMessage();
    // CseExceptions
} catch (Exception $e) {
    // Last Exception
} catch (Throwable $e) {
    // Last Throwable
}

Throw exception by code error

Examples:

ModelExceptions::throwException(ModelExceptions::ERROR_EXAMPLE_CODE_1);
// Error code 1

Add msg to exception:

ModelExceptions::throwException(ModelExceptions::ERROR_EXAMPLE_CODE_2, ' - msg test');
// Error code 2 - msg test

Default error msg:

ModelExceptions::throwException(4);
// Unknown error

Get error msg by code error

Examples:

ModelExceptions::getErrorMsg(ModelExceptions::ERROR_EXAMPLE_CODE_3);
// Error code 3

Default error msg:

ModelExceptions::getErrorMsg(4);
// Unknown error

Testing & Code Coverage

PHPUnit is used for unit testing. Unit tests ensure that class and methods does exactly what it is meant to do.

General PHPUnit documentation can be found at https://phpunit.de/documentation.html.

To run the PHPUnit unit tests, execute:

phpunit PATH/TO/PROJECT/tests/

If you want code coverage reports, use the following:

phpunit --coverage-html ./report PATH/TO/PROJECT/tests/

Used PHPUnit default config:

phpunit --configuration PATH/TO/PROJECT/phpunit.xml

Donating

You can support this project here. You can also help out by contributing to the project, or reporting bugs. Even voicing your suggestions for features is great. Anything to help is much appreciated.

License

The EXCEPTIONS CSE BASE is open-source PHP library licensed under the MIT license. Please see License File for more information.

GitHub @cs-eliseev