Comments (6)
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.
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.
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.
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.
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.
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)
- issue with php 7.3+ HOT 2
- State inconsistency with chained promises
- 1.4.0 Release HOT 4
- exception throwing HOT 4
- 'each' has deprecated since php 7.2 HOT 2
- PHP 8 : TypeError: method_exists(): Argument #1 ($object_or_class) must be of type object|string, null given HOT 3
- undefined method named "of" of class "GuzzleHttp\Promise\Coroutine" HOT 4
- each_limit skipping promises and failing with "Invoking the wait callback did not resolve the promise" HOT 9
- StreamHandler and HTTP/2 does not seem to work HOT 2
- Persist order of keys for Utils::settle HOT 1
- Cannot Bootstrap promise v.1.4.0
- Memory leak when using \GuzzleHttp\Pool with empty array of requests HOT 4
- EachPromise not running last item's callback HOT 3
- Add $config to the signatures of Utils::all and Each::of ?
- Grammatical errors HOT 1
- Allow all promise collection wrappers to dynamically add promises via $recursive option
- undefined function GuzzleHttp\Promise\all() HOT 1
- Make the promise interface a generic in phpDoc
- You had just realized factory of deffered and named in Promise...
- False positive warnings for each function
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from promises.