Coder Social home page Coder Social logo

Comments (6)

jsor avatar jsor commented on May 5, 2024 1

Exceptions thrown in fulfillment callbacks reject the promise returned from then(). You must call wait() on the returned promise.

$newPromise = $promise->then(
    function ($response) {
        throw new Exception('Invalid hit sent. got response: ' . $response->getBody());
    }
);

$newPromise->wait();

from promises.

PauloPhagula avatar PauloPhagula commented on May 5, 2024 1

Ohhhh sorry ... Didn't notice I have to wait on the new promise returned by then on the first promise.

Doing as suggested does work.

Thanks

from promises.

alexeyshockov avatar alexeyshockov commented on May 5, 2024

If this is the complete code, then the behaviour is valid. You have to call ->wait() on a promise to process the callback (task) queue. Or use some aggregation functions like all().

from promises.

PauloPhagula avatar PauloPhagula commented on May 5, 2024

I do call ->wait() after this bit to ensure the promise is processed. And it does get processed as later on I'm able to see the log message. My problem is that the exception does not bubble up, and thus I cannot know if there was an exception thrown or not.

Is it like within the task queue there's some sort of global error trapper?

from promises.

PauloPhagula avatar PauloPhagula commented on May 5, 2024

Probably I could clarify my issue with the sample bellow.

I have phpunit installed globally and guzzle installed as a library

require 'vendor/autoload.php';

use GuzzleHttp\Client;

class GuzzlePromisesTest extends \PHPUnit_Framework_TestCase
{
    /**
     * The http client
     * @var GuzzleHttp\ClientInterface
     */
    protected $httpClient;

    public function setUp() {
        $this->httpClient = new Client();
    }

    /**
     * @test
     * @expectedException Exception
     */
    public function should_trap_thrown_exception() {
        $promise = $this->httpClient->requestAsync('GET', 'https://www.google-analytics.com/debug/collect?v=1&aip=1&tid=UA-0000-00');

        $promise->then(
            function ($response){
                throw new Exception('Invalid hit sent. got response: ' . $response->getBody());
            }
        );

        $promise->wait();
    }
}
➜  tracking git:(use-guzzle-for-async-http-reqs) ✗ phpunit ~/Desktop/files/file.php
PHPUnit 5.1.3 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 1.69 seconds, Memory: 10.00Mb

There was 1 failure:

1) GuzzlePromisesTest::should_trap_thrown_exception
Failed asserting that exception of type "Exception" is thrown.

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

Note that if you navigate to the URL used in the test, you'll get a JSON response stating there are errors in the request data, but please mind the status code is 200. Meaning no "error" should be thrown by guzzle regarding the request.

However an error should be thrown from the promise handling. And that is what I expect to trap of the test

Thanks

from promises.

jsor avatar jsor commented on May 5, 2024

That's exactly covered by my comment #78 (comment) above.

You need to wait() on the returned promise.

    public function should_trap_thrown_exception() {
        $promise = $this->httpClient->requestAsync('GET', 'https://www.google-analytics.com/debug/collect?v=1&aip=1&tid=UA-0000-00');

        $newPromise = $promise->then(
            function ($response){
                throw new Exception('Invalid hit sent. got response: ' . $response->getBody());
            }
        );

        $newPromise->wait();
    }

from promises.

Related Issues (20)

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.