Coder Social home page Coder Social logo

evacuator's Introduction

Evacuator 3.0

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

evacuator's People

Contributors

serafimarts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.