FaultManagerdoc
You already may use PHP built-in exceptions or you might also have already build your own exceptions for your app, like for instance MyException
which implements Throwable interface.
Of course, you can still use the traditional way throw \MyException()
but you also can use :phpFault::throw()
or :phpFault::exception()
methods and get all the benefits of Fault Manager like features.custom-exceptions
and features.event-stream
.
try {
try {
try {
// Using Fault::throw()
Fault::throw(\Exception::class, 'Bad message');
} catch (\Exception $exception) {
// Some logic after catching \Exception
// Let's throw one of our own now with Fault::exception()
throw Fault::exception(\MyException::class, 'More info');
}
} catch (\MyException $exception) {
// Some logic goes here...
// Why not throw an already defined namespaced exception?
// Let's do it with Fault::throw()
Fault::throw(\MyApp\MyException::class, 'Too many exceptions!');
}
} catch (\MyApp\MyException::class $exception) {
// Some more logic here...
} finally {
// No more logic left! :D
}
Note
:phpFault::throw()
is an alias to :phpFault::exception()
with a difference that :phpFault::exception()
returns the exception and :phpFault::throw()
throws it directly. As you can see in the :phpFault
class on line 146
../../../src/Fault.php
As you may noticed, :phpFault::exception()
as also :phpFault::throw()
receive 5 parameters:
public static
- Parameters
- $exceptionClass (string)
- $message (string)
- $code (int)
- $previous (null | \Throwable <Throwable>)
- $arguments (array)
- :php
$exceptionClass
The name of the Exception class we want to throw - :php
$message
The message we want to deliver - :php
$code
The error code - :php
$previous
Previous Exception object - :php
$arguments
Array of items to replace in message text. See vsprintf
Since the first 4 parameters are self-explanatory we will focus on 5 th param :php$arguments
, a simple example:
Fault::throw(\Exception::class, 'Goodbye %s!', 0, null, ['world']);
The above example will produce a Fatal error: Uncaught Exception: Goodbye world!
As you probably realised already, is not very convenient to have :php$arguments
param at the end, but at least for time being, is better to not break the semantics of PHP's :phpException::__construct
.
public