@@ -191,8 +191,8 @@ public HttpResponse execute() throws IOException {
191191 }
192192
193193 @ Override
194- public void executeAsync (Consumer <? super HttpResponse > succeed ,
195- BiConsumer <? super HttpResponse , Throwable > fail , BiConsumer <? super HttpResponse , Throwable > always ) {
194+ public void executeAsync (Consumer <HttpResponse > succeed ,
195+ BiConsumer <HttpResponse , ? super IOException > fail , BiConsumer <HttpResponse , ? super IOException > always ) {
196196 executeImpl (new Response (succeed , fail , always ));
197197 }
198198
@@ -206,7 +206,7 @@ public void run() {
206206 fulfillPost (r );
207207 else
208208 fulfillGet (r );
209- } catch (Exception e ) {
209+ } catch (Throwable e ) {
210210 r .handleError (e );
211211 }
212212 }
@@ -220,8 +220,8 @@ public void run() {
220220 }
221221
222222 @ SuppressWarnings ("resource" )
223- public Response fulfillGet (Response r ) throws Exception {
224- URI uri = getUri () ;
223+ public Response fulfillGet (Response r ) throws IOException {
224+ URL url ;
225225 String data = "" ;
226226 for (Entry <String , String > e : htGetParams .entrySet ()) {
227227 String val = e .getValue ();
@@ -231,13 +231,15 @@ public Response fulfillGet(Response r) throws Exception {
231231 data += e .getKey () + "=" + val ;
232232 }
233233 if (data .length () > 0 ) {
234- uri = new URI (uri .toString () + "?" + data );
234+ url = new URL (uri .toString () + "?" + data );
235+ } else {
236+ url = uri .toURL ();
235237 }
236- return r .getResponse (getConnection (uri ), this );
238+ return r .getResponse (getConnection (url ), this );
237239 }
238240
239241 public Response fulfillPost (Response r ) throws IOException {
240- AjaxURLConnection conn = getConnection (uri );
242+ AjaxURLConnection conn = getConnection (uri . toURL () );
241243 for (int i = 0 ; i < listPostFiles .size (); i ++) {
242244 Object [] name_data = listPostFiles .get (i );
243245 String name = (String ) name_data [0 ];
@@ -276,9 +278,9 @@ public Response fulfillPost(Response r) throws IOException {
276278 * @param uri
277279 * @throws IOException
278280 */
279- private AjaxURLConnection getConnection (URI uri ) throws IOException {
281+ private AjaxURLConnection getConnection (URL url ) throws IOException {
280282 try {
281- AjaxURLConnection conn = (AjaxURLConnection ) uri . toURL () .openConnection ();
283+ AjaxURLConnection conn = (AjaxURLConnection ) url .openConnection ();
282284 conn .setUseCaches (false );
283285 if (!method .equals ("HEAD" ))
284286 conn .setDoInput (true );
@@ -308,7 +310,6 @@ public class Response implements HttpResponse {
308310
309311 ByteArrayInputStream inputStream ;
310312
311- private Throwable exception ;
312313
313314 /**
314315 * the HTTP(S)URLConnection that will handle this request, actually
@@ -321,16 +322,17 @@ public class Response implements HttpResponse {
321322 * asynchronous callback functions
322323 *
323324 */
324- private Consumer <? super HttpResponse > succeed ;
325- private BiConsumer <? super HttpResponse , Throwable > fail ;
326- private BiConsumer <? super HttpResponse , Throwable > always ;
325+ private Consumer <HttpResponse > succeed ;
326+ private BiConsumer <HttpResponse , ? super IOException > fail ;
327+ private BiConsumer <HttpResponse , ? super IOException > always ;
328+
327329
328330 private String method ;
329331
330332 private URI uri ;
331333
332- public Response (Consumer <? super HttpResponse > succeed , BiConsumer <? super HttpResponse , Throwable > fail ,
333- BiConsumer <? super HttpResponse , Throwable > always ) {
334+ public Response (Consumer <HttpResponse > succeed , BiConsumer <HttpResponse , ? super IOException > fail ,
335+ BiConsumer <HttpResponse , ? super IOException > always ) {
334336 this .succeed = succeed ;
335337 this .fail = fail ;
336338 this .always = always ;
@@ -368,19 +370,20 @@ Response getResponse(AjaxURLConnection conn, Request request) throws IOException
368370 @ Override
369371 public void run () {
370372 // asynchronous methods cannot throw an exception.
373+ IOException exception = null ;
371374 if (method .equals ("HEAD" )) {
372375 try {
373376 state = conn .getResponseCode ();
374377 } catch (IOException e ) {
375378 exception = e ;
376379 }
377- doCallback (state == 0 || state >= 400 );
380+ doCallback (state == 0 || state >= 400 , exception );
378381 } else {
379382 conn .getBytesAsync (new Function <byte [], Void >() {
380383
381384 @ Override
382385 public Void apply (byte [] t ) {
383- doCallback (t != null );
386+ doCallback (t != null , null );
384387 return null ;
385388 }
386389
@@ -396,14 +399,14 @@ public Void apply(byte[] t) {
396399 *
397400 * @param ok
398401 */
399- protected void doCallback (boolean ok ) {
400- ok &= (exception == null );
402+ protected void doCallback (boolean ok , IOException e ) {
403+ ok &= (e == null );
401404 if (ok && succeed != null )
402405 succeed .accept (this );
403406 else if (!ok && fail != null )
404- fail .accept (this , exception );
407+ fail .accept (this , e );
405408 if (always != null )
406- always .accept (this , exception );
409+ always .accept (this , e );
407410 }
408411
409412 /**
@@ -413,15 +416,19 @@ else if (!ok && fail != null)
413416 * @return true if aSynchronous and has been handled
414417 */
415418 protected boolean handleError (Throwable e ) {
416- exception = e ;
419+
420+ if (!(e instanceof IOException )) {
421+ e = new IOException (e );
422+ }
423+ IOException exception = (IOException ) e ;
417424 // setting e = null to indicated handled.
418425 if (isAsync ) {
419426 if (fail != null ) {
420- fail .accept (this , e );
427+ fail .accept (this , exception );
421428 e = null ;
422429 }
423430 if (always != null ) {
424- always .accept (this , e );
431+ always .accept (this , exception );
425432 e = null ;
426433 }
427434 }
@@ -470,11 +477,7 @@ public InputStream getContent() throws IOException {
470477
471478 @ Override
472479 public void close () {
473- try {
474- conn .disconnect ();
475- } catch (Throwable t ) {
476- // ignore
477- }
480+ conn .disconnect ();
478481 }
479482
480483 @ Override
0 commit comments