PHP code example of serafim / evacuator

1. Go to this page and download the library: Download serafim/evacuator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

serafim / evacuator example snippets


// 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)

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

$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();