Skip to content

Commit effd954

Browse files
committed
Set exception cause when re-raising IOException
This allows to execute custom logic in AsyncHttpResponseHandler.onFailure for specific errors, e.g. to display a nicer error message to the user when there is a DNS resolution failure, instead of leaking a useless and verbose UnknownHostException in the UI.
1 parent 9c7b6d4 commit effd954

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private void makeRequestWithRetries() throws IOException {
180180
// switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
181181
// while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
182182
// (to assist in genuine cases of unknown host) which seems better than outright failure
183-
cause = new IOException("UnknownHostException exception: " + e.getMessage());
183+
cause = new IOException("UnknownHostException exception: " + e.getMessage(), e);
184184
retry = (executionCount > 0) && retryHandler.retryRequest(e, ++executionCount, context);
185185
} catch (NullPointerException e) {
186186
// there's a bug in HttpClient 4.0.x that on some occasions causes
@@ -203,7 +203,7 @@ private void makeRequestWithRetries() throws IOException {
203203
} catch (Exception e) {
204204
// catch anything else to ensure failure message is propagated
205205
AsyncHttpClient.log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
206-
cause = new IOException("Unhandled exception: " + e.getMessage());
206+
cause = new IOException("Unhandled exception: " + e.getMessage(), cause);
207207
}
208208

209209
// cleaned up to throw IOException

0 commit comments

Comments
 (0)