1<p align="center">
2  <img src="https://static.hoa-project.net/Image/Hoa.svg" alt="Hoa" width="250px" />
3</p>
4
5---
6
7<p align="center">
8  <a href="https://travis-ci.org/hoaproject/exception"><img src="https://img.shields.io/travis/hoaproject/exception/master.svg" alt="Build status" /></a>
9  <a href="https://coveralls.io/github/hoaproject/exception?branch=master"><img src="https://img.shields.io/coveralls/hoaproject/exception/master.svg" alt="Code coverage" /></a>
10  <a href="https://packagist.org/packages/hoa/exception"><img src="https://img.shields.io/packagist/dt/hoa/exception.svg" alt="Packagist" /></a>
11  <a href="https://hoa-project.net/LICENSE"><img src="https://img.shields.io/packagist/l/hoa/exception.svg" alt="License" /></a>
12</p>
13<p align="center">
14  Hoa is a <strong>modular</strong>, <strong>extensible</strong> and
15  <strong>structured</strong> set of PHP libraries.<br />
16  Moreover, Hoa aims at being a bridge between industrial and research worlds.
17</p>
18
19# Hoa\Exception
20
21[![Help on IRC](https://img.shields.io/badge/help-%23hoaproject-ff0066.svg)](https://webchat.freenode.net/?channels=#hoaproject)
22[![Help on Gitter](https://img.shields.io/badge/help-gitter-ff0066.svg)](https://gitter.im/hoaproject/central)
23[![Documentation](https://img.shields.io/badge/documentation-hack_book-ff0066.svg)](https://central.hoa-project.net/Documentation/Library/Exception)
24[![Board](https://img.shields.io/badge/organisation-board-ff0066.svg)](https://waffle.io/hoaproject/exception)
25
26This library allows to use advanced exceptions. It provides generic exceptions
27(that are sent over the `hoa://Event/Exception` event channel), idle exceptions
28(that are not sent over an event channel), uncaught exception handlers, errors
29to exceptions handler and group of exceptions (with transactions).
30
31[Learn more](https://central.hoa-project.net/Documentation/Library/Exception).
32
33## Installation
34
35With [Composer](https://getcomposer.org/), to include this library into
36your dependencies, you need to
37require [`hoa/exception`](https://packagist.org/packages/hoa/exception):
38
39```sh
40$ composer require hoa/exception '~1.0'
41```
42
43For more installation procedures, please read [the Source
44page](https://hoa-project.net/Source.html).
45
46## Testing
47
48Before running the test suites, the development dependencies must be installed:
49
50```sh
51$ composer install
52```
53
54Then, to run all the test suites:
55
56```sh
57$ vendor/bin/hoa test:run
58```
59
60For more information, please read the [contributor
61guide](https://hoa-project.net/Literature/Contributor/Guide.html).
62
63## Quick usage
64
65We propose a quick overview of how to use generic exceptions, how to listen all
66thrown exceptions through events and how to use group of exceptions.
67
68### Generic exceptions
69
70An exception is constitued of:
71  * A message,
72  * A code (optional),
73  * A list of arguments for the message (à la `printf`, optional),
74  * A previous exception (optional).
75
76Thus, the following example builds an exception:
77
78```php
79$exception = new Hoa\Exception\Exception('Hello %s!', 0, 'world');
80```
81
82The exception message will be: `Hello world!`. The “raise” message (with all
83information, not only the message) is:
84
85```
86{main}: (0) Hello world!
87in … at line ….
88```
89
90Previous exceptions are shown too, for instance:
91
92```php
93$previous  = new Hoa\Exception\Exception('Hello previous.');
94$exception = new Hoa\Exception\Exception('Hello %s!', 0, 'world', $previous);
95
96echo $exception->raise(true);
97
98/**
99 * Will output:
100 *     {main}: (0) Hello world!
101 *     in … at line ….
102 *
103 *         ⬇
104 *
105 *     Nested exception (Hoa\Exception\Exception):
106 *     {main}: (0) Hello previous.
107 *     in … at line ….
108 */
109```
110
111### Listen exceptions through events
112
113Most exceptions in Hoa extend `Hoa\Exception\Exception`, which fire themselves
114on the `hoa://Event/Exception` event channel (please, see [the `Hoa\Event`
115library](http://central.hoa-project.net/Resource/Library/Event)). Consequently,
116we can listen for all exceptions that are thrown in the application by writing:
117
118```php
119Hoa\Event\Event::getEvent('hoa://Event/Exception')->attach(
120    function (Hoa\Event\Bucket $bucket) {
121        $exception = $bucket->getData();
122        // …
123    }
124);
125```
126
127Only the `Hoa\Exception\Idle` exceptions are not fired on the channel event.
128
129### Group and transactions
130
131Groups of exceptions are represented by the `Hoa\Exception\Group`. A group is an
132exception that contains one or many exceptions. A transactional API is provided
133to add more exceptions in the group with the following methods:
134  * `beginTransaction` to start a transaction,
135  * `rollbackTransaction` to remove all newly added exceptions since
136    `beginTransaction` call,
137  * `commitTransaction` to merge all newly added exceptions in the previous
138    transaction,
139  * `hasUncommittedExceptions` to check whether they are pending exceptions or
140    not.
141
142For instance, if an exceptional behavior is due to several reasons, a group of
143exceptions can be thrown instead of one exception. Group can be nested too,
144which is useful to represent a tree of exceptions. Thus:
145
146```php
147// A group of exceptions.
148$group           = new Hoa\Exception\Group('Failed because of several reasons.');
149$group['first']  = new Hoa\Exception\Exception('First reason');
150$group['second'] = new Hoa\Exception\Exception('Second reason');
151
152// Can nest another group.
153$group['third']           = new Hoa\Exception\Group('Third reason');
154$group['third']['fourth'] = new Hoa\Exception\Exception('Fourth reason');
155
156echo $group->raise(true);
157
158/**
159 * Will output:
160 *     {main}: (0) Failed because of several reasons.
161 *     in … at line ….
162 *
163 *     Contains the following exceptions:
164 *
165 *       • {main}: (0) First reason
166 *         in … at line ….
167 *
168 *       • {main}: (0) Second reason
169 *         in … at line ….
170 *
171 *       • {main}: (0) Third reason
172 *         in … at line ….
173 *
174 *         Contains the following exceptions:
175 *
176 *           • {main}: (0) Fourth reason
177 *             in … at line ….
178 */
179```
180
181The following example uses a transaction to add new exceptions in the group:
182
183```php
184$group   = new Hoa\Exception\Group('Failed because of several reasons.');
185$group[] = new Hoa\Exception\Exception('Always present.');
186
187$group->beginTransaction();
188
189$group[] = new Hoa\Exception\Exception('Might be present.');
190
191if (true === $condition) {
192    $group->commitTransaction();
193} else {
194    $group->rollbackTransaction();
195}
196```
197
198## Documentation
199
200The
201[hack book of `Hoa\Exception`](https://central.hoa-project.net/Documentation/Library/Exception)
202contains detailed information about how to use this library and how it works.
203
204To generate the documentation locally, execute the following commands:
205
206```sh
207$ composer require --dev hoa/devtools
208$ vendor/bin/hoa devtools:documentation --open
209```
210
211More documentation can be found on the project's website:
212[hoa-project.net](https://hoa-project.net/).
213
214## Getting help
215
216There are mainly two ways to get help:
217
218  * On the [`#hoaproject`](https://webchat.freenode.net/?channels=#hoaproject)
219    IRC channel,
220  * On the forum at [users.hoa-project.net](https://users.hoa-project.net).
221
222## Contribution
223
224Do you want to contribute? Thanks! A detailed [contributor
225guide](https://hoa-project.net/Literature/Contributor/Guide.html) explains
226everything you need to know.
227
228## License
229
230Hoa is under the New BSD License (BSD-3-Clause). Please, see
231[`LICENSE`](https://hoa-project.net/LICENSE) for details.
232