PHP code example of transprime-research / attempt

1. Go to this page and download the library: Download transprime-research/attempt 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/ */

    

transprime-research / attempt example snippets


$response = attempt(fn() => $this->client->get('ninja'))
    ->catch(ConnectException::class)
    ->done(fn() => []); //done can be replaced with ()

$response = Attempt::on(fn() => $this->client->get('ninja'))
    ->catch(AttemptTestException::class)(); // or ->done()

// Do something with Response

$response = Attempt::on(fn() => $this->client->get('ninja'))
    ->catch(\AttemptTestException())(); // or ->done()

// Do something with Response

$response = Attempt::on(fn() => $this->client->get('ninja'))
    ->with(['abc'])
    ->catch(AttemptTestException::class)
    ->done(); // ['abc'] is returned if exception is caught  

// with default value
attempt(fn() => $this->execute())
    ->catch(AttemptTestException::class, 'It is done') //returns 'It is done'
    ->done();
    
// closure as default value
attempt(fn() => $this->execute())
    ->catch(AttemptTestException::class, fn() => 'It is done') //returns 'It is done'
    ->done();
    
// handle the resolved default value in done()
attempt(fn() => $this->execute())
    ->catch(AttemptTestException::class, fn() => 'error') //returns 'It is done'
    ->done(fn(Exception $ex, $severity) => logger($severity, $ex));

$response = Attempt::on(fn() => $this->client->get('ninja'))
    ->catch(AttemptTestException::class, HttpResponseException::class)()

// Do something with Response

attempt(fn() => $this->execute())
    ->catch(NinjaException::class, 'Ninja error') //returns 'It is done')
    ->catch(AnotherExeption::class, 'Another error') //returns 'It is done'
    ->done(fn($ex) => logger()->error($ex));

$response = Attempt::on(fn() => $this->client->get('ninja'))
    ->catch(AttemptTestException::class, HttpResponseException::class)
    ->done(fn(\HttpResponseException $e) => logger()->error($e->getMessage()));

// Do something with Response

// loading...