File tree Expand file tree Collapse file tree 2 files changed +47
-5
lines changed Expand file tree Collapse file tree 2 files changed +47
-5
lines changed Original file line number Diff line number Diff line change @@ -185,7 +185,12 @@ public function fulfill()
185
185
$ this ->state = Promise::FULFILLED ;
186
186
$ response = $ this ->responseBuilder ->getResponse ();
187
187
$ response ->getBody ()->seek (0 );
188
- $ response = $ this ->call ($ this ->onFulfilled , $ response );
188
+
189
+ while (count ($ this ->onFulfilled ) > 0 ) {
190
+ $ callback = array_shift ($ this ->onFulfilled );
191
+ $ response = call_user_func ($ callback , $ response );
192
+ }
193
+
189
194
if ($ response instanceof ResponseInterface) {
190
195
$ this ->responseBuilder ->setResponse ($ response );
191
196
}
@@ -201,10 +206,14 @@ public function reject(Exception $exception)
201
206
$ this ->exception = $ exception ;
202
207
$ this ->state = Promise::REJECTED ;
203
208
204
- try {
205
- $ this ->call ($ this ->onRejected , $ this ->exception );
206
- } catch (Exception $ exception ) {
207
- $ this ->exception = $ exception ;
209
+ while (count ($ this ->onRejected ) > 0 ) {
210
+ $ callback = array_shift ($ this ->onRejected );
211
+ try {
212
+ $ exception = call_user_func ($ callback , $ this ->exception );
213
+ $ this ->exception = $ exception ;
214
+ } catch (Exception $ exception ) {
215
+ $ this ->exception = $ exception ;
216
+ }
208
217
}
209
218
}
210
219
Original file line number Diff line number Diff line change @@ -83,6 +83,39 @@ function (RequestException $exception) {
83
83
static ::assertEquals ('Bar ' , $ core ->getException ()->getMessage ());
84
84
}
85
85
86
+ /**
87
+ * «onReject» callback can throw exception.
88
+ *
89
+ * @see https://github.com/php-http/curl-client/issues/26
90
+ */
91
+ public function testIssue26 ()
92
+ {
93
+ $ request = $ this ->createRequest ('GET ' , '/ ' );
94
+ $ this ->handle = curl_init ();
95
+
96
+ $ core = new PromiseCore (
97
+ $ request ,
98
+ $ this ->handle ,
99
+ new ResponseBuilder ($ this ->createResponse ())
100
+ );
101
+ $ core ->addOnRejected (
102
+ function (RequestException $ exception ) {
103
+ throw new RequestException ('Foo ' , $ exception ->getRequest (), $ exception );
104
+ }
105
+ );
106
+ $ core ->addOnRejected (
107
+ function (RequestException $ exception ) {
108
+ return new RequestException ('Bar ' , $ exception ->getRequest (), $ exception );
109
+ }
110
+ );
111
+
112
+ $ exception = new RequestException ('Error ' , $ request );
113
+ $ core ->reject ($ exception );
114
+ static ::assertEquals (Promise::REJECTED , $ core ->getState ());
115
+ static ::assertInstanceOf (Exception::class, $ core ->getException ());
116
+ static ::assertEquals ('Bar ' , $ core ->getException ()->getMessage ());
117
+ }
118
+
86
119
/**
87
120
* @expectedException \LogicException
88
121
*/
You can’t perform that action at this time.
0 commit comments