serafim/evacuator

Try-catch with multiple retries

3.0.0 2016-12-29 19:09 UTC

This package is auto-updated.

Last update: 2024-12-07 15:59:19 UTC


README

Build Status Code Quality

He trying to save your code, if it is broken :3

Installation

composer require serafim/evacuator

Link to Packagist

Usage example

// What we are trying to keep safe?
$result = rescue(function () { 
    if (random_int(0, 9999) > 1) {
        throw new \Exception('Ooooups =(');
    }
    
    return 23;
});

var_dump($result); // int(23)

Advanced usage

use Serafim\Evacuator\Evacuator;

$result = (new Evacuator(function() {

    // Your a very important piece of code

}))

    // Code throws an exception after 100500 call retries 
    ->retry(100500) 
    
    // or until the cancer is not on the mountain whistles...
    ->retry(Evacuator::INFINITY_RETRIES) 
    
    // But if you want catch exception
    ->catch(function (Throwable $e) {
        return 'Something went wrong =('; // Will be returns into $result
    })
    
    ->finally(function ($errorOrResult) {
        // Run this code after all attempts.
        // $errorOrResult can be error (if evacuator cant keep safe your code) or result value
    })
    
    ->onError(function ($error) {
        // Run this code after every error
    })
    
    ->invoke(); // Just run your very important code

Catching strategy

$result = (new Evacuator(function() {
    throw new \LogicException('Error');
}))

    ->catch(function (\RuntimeException $e) {
        // I am alone and never be use ='( 
    })
    // ->onError(function (\RuntimeException $e) {})
    
    ->catch(function (\LogicException $e) {
        // Yay! I will calling because Im a LogicException! :D
    })
    // ->onError(function (\LogicException $e) {})
    
    ->invoke();

Enjoy! :3