Skip to content

Commit 1979217

Browse files
committed
Merge pull request android-async-http#795 from fineswap/UserExceptionsHandler
Define a new method to catch user-space exceptions and report them.
2 parents 6472ec5 + cb06265 commit 1979217

File tree

1 file changed

+54
-45
lines changed

1 file changed

+54
-45
lines changed

library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ public void onCancel() {
307307
Log.d(LOG_TAG, "Request got cancelled");
308308
}
309309

310+
public void onUserException(Throwable error) {
311+
Log.e(LOG_TAG, "User-space exception detected!", error);
312+
throw new RuntimeException(error);
313+
}
314+
310315
@Override
311316
final public void sendProgressMessage(int bytesWritten, int bytesTotal) {
312317
sendMessage(obtainMessage(PROGRESS_MESSAGE, new Object[]{bytesWritten, bytesTotal}));
@@ -346,52 +351,56 @@ final public void sendCancelMessage() {
346351
protected void handleMessage(Message message) {
347352
Object[] response;
348353

349-
switch (message.what) {
350-
case SUCCESS_MESSAGE:
351-
response = (Object[]) message.obj;
352-
if (response != null && response.length >= 3) {
353-
onSuccess((Integer) response[0], (Header[]) response[1], (byte[]) response[2]);
354-
} else {
355-
Log.e(LOG_TAG, "SUCCESS_MESSAGE didn't got enough params");
356-
}
357-
break;
358-
case FAILURE_MESSAGE:
359-
response = (Object[]) message.obj;
360-
if (response != null && response.length >= 4) {
361-
onFailure((Integer) response[0], (Header[]) response[1], (byte[]) response[2], (Throwable) response[3]);
362-
} else {
363-
Log.e(LOG_TAG, "FAILURE_MESSAGE didn't got enough params");
364-
}
365-
break;
366-
case START_MESSAGE:
367-
onStart();
368-
break;
369-
case FINISH_MESSAGE:
370-
onFinish();
371-
break;
372-
case PROGRESS_MESSAGE:
373-
response = (Object[]) message.obj;
374-
if (response != null && response.length >= 2) {
375-
try {
376-
onProgress((Integer) response[0], (Integer) response[1]);
377-
} catch (Throwable t) {
378-
Log.e(LOG_TAG, "custom onProgress contains an error", t);
354+
try {
355+
switch (message.what) {
356+
case SUCCESS_MESSAGE:
357+
response = (Object[]) message.obj;
358+
if (response != null && response.length >= 3) {
359+
onSuccess((Integer) response[0], (Header[]) response[1], (byte[]) response[2]);
360+
} else {
361+
Log.e(LOG_TAG, "SUCCESS_MESSAGE didn't got enough params");
379362
}
380-
} else {
381-
Log.e(LOG_TAG, "PROGRESS_MESSAGE didn't got enough params");
382-
}
383-
break;
384-
case RETRY_MESSAGE:
385-
response = (Object[]) message.obj;
386-
if (response != null && response.length == 1) {
387-
onRetry((Integer) response[0]);
388-
} else {
389-
Log.e(LOG_TAG, "RETRY_MESSAGE didn't get enough params");
390-
}
391-
break;
392-
case CANCEL_MESSAGE:
393-
onCancel();
394-
break;
363+
break;
364+
case FAILURE_MESSAGE:
365+
response = (Object[]) message.obj;
366+
if (response != null && response.length >= 4) {
367+
onFailure((Integer) response[0], (Header[]) response[1], (byte[]) response[2], (Throwable) response[3]);
368+
} else {
369+
Log.e(LOG_TAG, "FAILURE_MESSAGE didn't got enough params");
370+
}
371+
break;
372+
case START_MESSAGE:
373+
onStart();
374+
break;
375+
case FINISH_MESSAGE:
376+
onFinish();
377+
break;
378+
case PROGRESS_MESSAGE:
379+
response = (Object[]) message.obj;
380+
if (response != null && response.length >= 2) {
381+
try {
382+
onProgress((Integer) response[0], (Integer) response[1]);
383+
} catch (Throwable t) {
384+
Log.e(LOG_TAG, "custom onProgress contains an error", t);
385+
}
386+
} else {
387+
Log.e(LOG_TAG, "PROGRESS_MESSAGE didn't got enough params");
388+
}
389+
break;
390+
case RETRY_MESSAGE:
391+
response = (Object[]) message.obj;
392+
if (response != null && response.length == 1) {
393+
onRetry((Integer) response[0]);
394+
} else {
395+
Log.e(LOG_TAG, "RETRY_MESSAGE didn't get enough params");
396+
}
397+
break;
398+
case CANCEL_MESSAGE:
399+
onCancel();
400+
break;
401+
}
402+
} catch(Throwable error) {
403+
onUserException(error);
395404
}
396405
}
397406

0 commit comments

Comments
 (0)