Giter Club home page Giter Club logo

exception's Introduction

Hoa


Build status Code coverage Packagist License

Hoa is a modular, extensible and structured set of PHP libraries.
Moreover, Hoa aims at being a bridge between industrial and research worlds.

Hoa\Exception

Help on IRC Help on Gitter Documentation Board

This library allows to use advanced exceptions. It provides generic exceptions (that are sent over the hoa://Event/Exception event channel), idle exceptions (that are not sent over an event channel), uncaught exception handlers, errors to exceptions handler and group of exceptions (with transactions).

Learn more.

Installation

With Composer, to include this library into your dependencies, you need to require hoa/exception:

$ composer require hoa/exception '~2.0'

For more installation procedures, please read the Source page.

Testing

Before running the test suites, the development dependencies must be installed:

$ composer install

Then, to run all the test suites:

$ vendor/bin/hoa test:run

For more information, please read the contributor guide.

Quick usage

We propose a quick overview of how to use generic exceptions, how to listen all thrown exceptions through events and how to use group of exceptions.

Generic exceptions

An exception is constitued of:

  • A message,
  • A code (optional),
  • A list of arguments for the message (à la printf, optional),
  • A previous exception (optional).

Thus, the following example builds an exception:

$exception = new Hoa\Exception\Exception('Hello %s!', 0, 'world');

The exception message will be: Hello world!. The “raise” message (with all information, not only the message) is:

{main}: (0) Hello world!
in … at line ….

Previous exceptions are shown too, for instance:

$previous  = new Hoa\Exception\Exception('Hello previous.');
$exception = new Hoa\Exception\Exception('Hello %s!', 0, 'world', $previous);

echo $exception->raise(true);

/**
 * Will output:
 *     {main}: (0) Hello world!
 *     in … at line ….
 *     
 *         ⬇
 *     
 *     Nested exception (Hoa\Exception\Exception):
 *     {main}: (0) Hello previous.
 *     in … at line ….
 */

Listen exceptions through events

Most exceptions in Hoa extend Hoa\Exception\Exception, which fire themselves on the hoa://Event/Exception event channel (please, see the Hoa\Event library). Consequently, we can listen for all exceptions that are thrown in the application by writing:

Hoa\Event\Event::getEvent('hoa://Event/Exception')->attach(
    function (Hoa\Event\Bucket $bucket) {
        $exception = $bucket->getData();
        // …
    }
);

Only the Hoa\Exception\Idle exceptions are not fired on the channel event.

Group and transactions

Groups of exceptions are represented by the Hoa\Exception\Group. A group is an exception that contains one or many exceptions. A transactional API is provided to add more exceptions in the group with the following methods:

  • beginTransaction to start a transaction,
  • rollbackTransaction to remove all newly added exceptions since beginTransaction call,
  • commitTransaction to merge all newly added exceptions in the previous transaction,
  • hasUncommittedExceptions to check whether they are pending exceptions or not.

For instance, if an exceptional behavior is due to several reasons, a group of exceptions can be thrown instead of one exception. Group can be nested too, which is useful to represent a tree of exceptions. Thus:

// A group of exceptions.
$group           = new Hoa\Exception\Group('Failed because of several reasons.');
$group['first']  = new Hoa\Exception\Exception('First reason');
$group['second'] = new Hoa\Exception\Exception('Second reason');

// Can nest another group.
$group['third']           = new Hoa\Exception\Group('Third reason');
$group['third']['fourth'] = new Hoa\Exception\Exception('Fourth reason');

echo $group->raise(true);

/**
 * Will output:
 *     {main}: (0) Failed because of several reasons.
 *     in … at line ….
 *     
 *     Contains the following exceptions:
 *     
 *       • {main}: (0) First reason
 *         in … at line ….
 *     
 *       • {main}: (0) Second reason
 *         in … at line ….
 *     
 *       • {main}: (0) Third reason
 *         in … at line ….
 *         
 *         Contains the following exceptions:
 *         
 *           • {main}: (0) Fourth reason
 *             in … at line ….
 */

The following example uses a transaction to add new exceptions in the group:

$group   = new Hoa\Exception\Group('Failed because of several reasons.');
$group[] = new Hoa\Exception\Exception('Always present.');

$group->beginTransaction();

$group[] = new Hoa\Exception\Exception('Might be present.');

if (true === $condition) {
    $group->commitTransaction();
} else {
    $group->rollbackTransaction();
}

Documentation

The hack book of Hoa\Exception contains detailed information about how to use this library and how it works.

To generate the documentation locally, execute the following commands:

$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open

More documentation can be found on the project's website: hoa-project.net.

Getting help

There are mainly two ways to get help:

Contribution

Do you want to contribute? Thanks! A detailed contributor guide explains everything you need to know.

License

Hoa is under the New BSD License (BSD-3-Clause). Please, see LICENSE for details.

exception's People

Contributors

hywan avatar metalaka avatar shulard avatar vonglasow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

exception's Issues

Group behavior beside Event

I've a little situation when working with an Event handler on Exception and play with Group.

All the hoa exceptions are dispatch to event during execution of constructor.
that mean when working with Group, the exception are dispatch as like it was common exception.

And it's not what we should expect when working with Group, we may want analyse or raise the group in one time, and when child exceptions are in.

Maybe we could send event when Group::commitTransaction are call ?
and do not send event in constructor when it's Exception\Group instance,
but that mean it's a BC-Break

From now i found this great solution.

  1. Use Exception\Idle as child of Group
  2. In event handler, ignore Exception\Group when 0 === $exception->getStackSize()
  3. Dispatch Exception\Group with $exception->send() when you're setup.

Dependabot can't resolve your PHP dependency files

Dependabot can't resolve your PHP dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Your requirements could not be resolved to an installable set of packages.
  Problem 1
    - Root composer.json requires hoa/event dev-master -> satisfiable by hoa/event[dev-master].
    - hoa/event dev-master requires hoa/exception dev-master -> found hoa/exception[1.0.0+no-version-set] but it does not match the constraint.
  Problem 2
    - Root composer.json requires hoa/test dev-master -> satisfiable by hoa/test[dev-master].
    - hoa/test dev-master requires hoa/cli dev-master -> found hoa/cli[dev-master, 3.x-dev (alias of dev-master)] but it does not match your minimum-stability.

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Dependabot can't resolve your PHP dependency files

Dependabot can't resolve your PHP dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Your requirements could not be resolved to an installable set of packages.
  Problem 1
    - Root composer.json requires hoa/event dev-master -> satisfiable by hoa/event[dev-master].
    - hoa/event dev-master requires hoa/exception dev-master -> found hoa/exception[1.0.0+no-version-set] but it does not match the constraint.
  Problem 2
    - Root composer.json requires hoa/test dev-master -> satisfiable by hoa/test[dev-master].
    - hoa/test dev-master requires hoa/cli dev-master -> found hoa/cli[dev-master, 3.x-dev (alias of dev-master)] but it does not match your minimum-stability.

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Events not raised?

Hello,

In hoa_test.php I have the following part:

$previous  = new \Hoa\Exception\Exception('Hello previous.');
$exception = new \Hoa\Exception\Exception('Hello %s!', 0, ['world'], $previous);

$exception->raise(true);

\Hoa\Event\Event::getEvent('hoa://Event/Exception')->attach(function (\Hoa\Event\Bucket $bucket) {
    $exception = $bucket->getData();

    \var_dump($exception);
});

when I run $ php hoa_test.php I don't receive any output...

Am I doing something wrong?

Thanks!

PS: echo $exception->raise(true); works fine, the part that does not work for me is the event listener.

Dependabot can't resolve your PHP dependency files

Dependabot can't resolve your PHP dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Your requirements could not be resolved to an installable set of packages.
  Problem 1
    - Installation request for hoa/event dev-master -> satisfiable by hoa/event[dev-master].
    - hoa/event dev-master requires hoa/exception dev-master -> satisfiable by hoa/exception[dev-master] but these conflict with your requirements or minimum-stability.
  Problem 2
    - Installation request for hoa/test dev-master -> satisfiable by hoa/test[dev-master].
    - hoa/test dev-master requires hoa/cli dev-master -> satisfiable by hoa/cli[dev-master] but these conflict with your requirements or minimum-stability.

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

You can mention @dependabot in the comments below to contact the Dependabot team.

Dependabot can't resolve your PHP dependency files

Dependabot can't resolve your PHP dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Your requirements could not be resolved to an installable set of packages.
  Problem 1
    - Installation request for hoa/event dev-master -> satisfiable by hoa/event[dev-master].
    - hoa/event dev-master requires hoa/exception dev-master -> satisfiable by hoa/exception[dev-master] but these conflict with your requirements or minimum-stability.
  Problem 2
    - Installation request for hoa/test dev-master -> satisfiable by hoa/test[dev-master].
    - hoa/test dev-master requires hoa/cli dev-master -> satisfiable by hoa/cli[dev-master] but these conflict with your requirements or minimum-stability.

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

You can mention @dependabot in the comments below to contact the Dependabot team.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.