Runs a callable, catching any exception and reflecting it into a triggered error.

This allows you to combine exception generating code with retry code to trace the execution over the retry cycle. Example:


$retry = new \Haldayne\Fox\Retry(
     $capture = new \Haldayne\Fox\CaptureErrors(
         new \Haldayne\Fox\ExceptionToError(
             function () { throw new \Exception('Oops!', rand(1,10)); }
         )
     )
);
$result = $retry();
if (false === $result) {
    die(
        $capture->
        getCapturedErrors()->
        pluck('message')->
        into(new MapOfStrings)->
        join(PHP_EOL)
    );
}
package Haldayne

 Methods

Create a new callable object that captures any exception when the given callable is invoked.

__construct(callable $code) 

Parameters

$code

callable

Invoke the helper. Passes any arguments into the callable given during helper construction. Returns any result from the callable, unless the callable raises an exception: in which case, returns false.

__invoke() 

Pass the given exception into the error message format string.

formatException(\Exception $ex) 

Parameters

$ex

Gets the current error code.

getErrorCode() 

Gets the current error message format.

getErrorMessageFormat() 

Change the code used to deliver the exceptions. By default, exceptions converted to E_USER_ERROR errors.

setErrorCode($code) 

Parameters

$code

Change the format of the generated error messages. Accepts a printf- style format string, where the following parameters are replaced: - %1$s The exception message - %2$d The exception code - %3$s The file where the exception occurred - %4$d The line where the exception occurred - %5$s The class of the raised exception

setErrorMessageFormat($format) 

The default format is: %1$s (%2$d) thrown at %3$s:%4$d as %5$s exception.

Parameters

$format

 Properties

 

$code

$code 

Default

null
 

$errorCode

$errorCode 

Default

E_USER_ERROR
 

$errorMessageFormat

$errorMessageFormat 

Default

'%1$s (%2$d) thrown at %3$s:%4$d as %5$s exception.'