Browse Source

Add android timeout exception handling. #44

Fix Blob constructor error
Ben Hsieh 8 years ago
parent
commit
5430f4ff24

+ 26
- 7
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

@@ -82,6 +82,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
82 82
     RequestType requestType;
83 83
     ResponseType responseType;
84 84
     boolean timeout = false;
85
+    WritableMap respInfo;
85 86
 
86 87
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
87 88
         this.method = method;
@@ -250,7 +251,12 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
250 251
                     break;
251 252
 
252 253
                 case WithoutBody:
253
-                    builder.method(method, null);
254
+                    if(method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("PUT"))
255
+                    {
256
+                        builder.method(method, RequestBody.create(null, new byte[0]));
257
+                    }
258
+                    else
259
+                        builder.method(method, null);
254 260
                     break;
255 261
             }
256 262
 
@@ -291,23 +297,36 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
291 297
                     return chain.proceed(chain.request());
292 298
                 }
293 299
             });
294
-            
295
-            if(options.timeout != -1) {
300
+
301
+            if(options.timeout > 0) {
296 302
                 clientBuilder.connectTimeout(options.timeout, TimeUnit.SECONDS);
303
+                clientBuilder.readTimeout(options.timeout, TimeUnit.SECONDS);
297 304
             }
298 305
 
299 306
             OkHttpClient client = clientBuilder.build();
300 307
             Call call = client.newCall(req);
301 308
             taskTable.put(taskId, call);
302 309
             call.enqueue(new okhttp3.Callback() {
310
+
303 311
                 @Override
304 312
                 public void onFailure(Call call, IOException e) {
305
-                    callback.invoke(e.getLocalizedMessage(), null);
313
+                    if(respInfo == null) {
314
+                        respInfo = Arguments.createMap();
315
+                    }
316
+
317
+                    // check if this error caused by timeout
318
+                    if(e.getClass().equals(SocketTimeoutException.class)) {
319
+                        respInfo.putBoolean("timeout", true);
320
+                        callback.invoke("request timed out.", respInfo, null);
321
+                    }
322
+                    else
323
+                        callback.invoke(e.getLocalizedMessage(), respInfo, null);
306 324
                 }
307 325
 
308 326
                 @Override
309 327
                 public void onResponse(Call call, Response response) throws IOException {
310 328
                     ReadableMap notifyConfig = options.addAndroidDownloads;
329
+                    respInfo = getResponseInfo(response);
311 330
                     // Download manager settings
312 331
                     if(notifyConfig != null ) {
313 332
                         String title = "", desc = "", mime = "text/plain";
@@ -333,7 +352,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
333 352
         } catch (Exception error) {
334 353
             error.printStackTrace();
335 354
             taskTable.remove(taskId);
336
-            callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause());
355
+            callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause(), this.respInfo);
337 356
         }
338 357
     }
339 358
 
@@ -405,10 +424,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
405 424
         }
406 425
         info.putMap("headers", headers);
407 426
         Headers h = resp.headers();
408
-        if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("text/plain")) {
427
+        if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("text/")) {
409 428
             info.putString("respType", "text");
410 429
         }
411
-        else if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("application/json")) {
430
+        else if(getHeaderIgnoreCases(h, "content-type").contains("application/json")) {
412 431
             info.putString("respType", "json");
413 432
         }
414 433
         else if(getHeaderIgnoreCases(h, "content-type").length() < 1) {

+ 1
- 0
src/index.js View File

@@ -150,6 +150,7 @@ function fetch(...args:any):Promise {
150 150
       subscription.remove()
151 151
       subscriptionUpload.remove()
152 152
       stateEvent.remove()
153
+      info = info ? info : {}
153 154
       if(err)
154 155
         reject(new Error(err, info))
155 156
       else {

+ 1
- 0
src/polyfill/Blob.js View File

@@ -47,6 +47,7 @@ export default class Blob extends EventTarget {
47 47
    *                    by default
48 48
    */
49 49
   constructor(data:any, cType:any) {
50
+    super()
50 51
     cType = cType || {}
51 52
     this.cacheName = getBlobName()
52 53
     this.isRNFetchBlobPolyfill = true

+ 2
- 2
src/polyfill/XMLHttpRequest.js View File

@@ -124,7 +124,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
124 124
       body = JSON.stringify(body)
125 125
     }
126 126
     else
127
-      body = body.toString()
127
+      body = body ? body.toString() : body
128 128
 
129 129
     this._task = RNFetchBlob
130 130
                   .config({ auto: true, timeout : this._timeout })
@@ -299,7 +299,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
299 299
     this._onreadystatechange = fn
300 300
   }
301 301
 
302
-  get onreadystatechange(fn:() => void) {
302
+  get onreadystatechange() {
303 303
     return this._onreadystatechange
304 304
   }
305 305
 

+ 1
- 0
src/polyfill/index.js View File

@@ -3,6 +3,7 @@ import File from './File.js'
3 3
 import XMLHttpRequest from './XMLHttpRequest.js'
4 4
 import FormData from './FormData.js'
5 5
 import ProgressEvent from './ProgressEvent'
6
+import Event from './Event'
6 7
 
7 8
 export default {
8 9
   Blob, File, XMLHttpRequest, FormData, ProgressEvent, Event