Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import java.io.IOException; | 7 import java.io.IOException; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Exception after UrlRequest start. Could be reported by network stack, in | 10 * Exception passed to {@link UrlRequestListener#onFailed UrlRequestListener.onF ailed()} when: |
| 11 * which case netError() will contain native error code. | 11 * <ul> |
| 12 * <li>{@link UrlRequestListener} or {@link UploadDataProvider} method throws an exception. In this | |
| 13 * case {@link IOException#getCause getCause()} can be used to find the thro wn exception. | |
| 14 * {@link #getErrorCode} will return {@link #ERROR_LISTENER_THREW}. | |
| 15 * <li>Cronet fails to process a network request. In this case | |
|
xunjieli
2015/11/06 15:23:22
nit: This sentence seems a bit too short. Maybe mo
pauljensen
2016/01/25 01:43:50
Hmm, ya, it is a bit short. I don't have any grea
| |
| 16 * {@link #getErrorCode} and {@link #netError} can be used to get more | |
|
xunjieli
2015/11/06 15:23:22
nit: netError -> getCronetInternalErrorCode
pauljensen
2016/01/25 01:43:50
Done.
| |
| 17 * information about the specific type of failure. | |
| 18 * </ul> | |
| 12 */ | 19 */ |
| 13 public class UrlRequestException extends IOException { | 20 public class UrlRequestException extends IOException { |
| 14 /** Net error code if exception is reported by native. */ | 21 /** |
| 15 final int mNetError; | 22 * Error code indicating this class wraps an exception thrown by {@link UrlR equestListener} or |
| 23 * {@link UploadDataProvider}. Wrapped exception can be retrieved using | |
| 24 * {@link IOException#getCause}. | |
| 25 */ | |
| 26 public static final int ERROR_LISTENER_THREW = UrlRequestError.LISTENER_THRE W; | |
| 27 /** | |
| 28 * Error code indicating the host being sent the request could not be resolv ed to an IP address. | |
| 29 */ | |
| 30 public static final int ERROR_HOSTNAME_NOT_RESOLVED = UrlRequestError.HOSTNA ME_NOT_RESOLVED; | |
| 31 /** | |
| 32 * Error code indicating the device was not connected to any network. | |
| 33 */ | |
| 34 public static final int ERROR_INTERNET_DISCONNECTED = UrlRequestError.INTERN ET_DISCONNECTED; | |
| 35 /** | |
| 36 * Error code indicating that as the request was processed the network confi guration chnaged. | |
|
xunjieli
2015/11/06 15:23:22
nit: typo in "changed"
pauljensen
2016/01/25 01:43:50
Done.
| |
| 37 */ | |
| 38 public static final int ERROR_NETWORK_CHANGED = UrlRequestError.NETWORK_CHAN GED; | |
| 39 /** | |
| 40 * Error code indicating a timeout expired. Timeouts expiring while attempti ng to connect will | |
| 41 * be reported as the more specific {@link #ERROR_CONNECTION_TIMED_OUT}. | |
| 42 */ | |
| 43 public static final int ERROR_TIMED_OUT = UrlRequestError.TIMED_OUT; | |
| 44 /** | |
| 45 * Error code indicating the connection was closed unexpectedly. | |
| 46 */ | |
| 47 public static final int ERROR_CONNECTION_CLOSED = UrlRequestError.CONNECTION _CLOSED; | |
| 48 /** | |
| 49 * Error code indicating the connection attempt timed out. | |
| 50 */ | |
| 51 public static final int ERROR_CONNECTION_TIMED_OUT = UrlRequestError.CONNECT ION_TIMED_OUT; | |
| 52 /** | |
| 53 * Error code indicating the connection attempt was refused. | |
| 54 */ | |
| 55 public static final int ERROR_CONNECTION_REFUSED = UrlRequestError.CONNECTIO N_REFUSED; | |
| 56 /** | |
| 57 * Error code indicating the connection was unexpectedly reset. | |
| 58 */ | |
| 59 public static final int ERROR_CONNECTION_RESET = UrlRequestError.CONNECTION_ RESET; | |
| 60 /** | |
| 61 * Error code indicating the IP address being contacted is unreachable, mean ing there is no | |
| 62 * route to the specified host or network. | |
| 63 */ | |
| 64 public static final int ERROR_ADDRESS_UNREACHABLE = UrlRequestError.ADDRESS_ UNREACHABLE; | |
| 65 /** | |
| 66 * Error code indicating another type of error was encountered. {@link #netE rror} can be | |
|
xunjieli
2015/11/06 15:23:22
nit: netError -> getCronetInternalErrorCode?
pauljensen
2016/01/25 01:43:50
Done.
| |
| 67 * consulted to get a more specific cause. | |
| 68 */ | |
| 69 public static final int ERROR_OTHER = UrlRequestError.OTHER; | |
| 16 | 70 |
| 17 UrlRequestException(String message, Throwable cause) { | 71 // Error code, one of ERROR_* |
| 72 private final int mErrorCode; | |
| 73 // Cronet internal error code. | |
| 74 private final int mCronetInternalErrorCode; | |
| 75 | |
| 76 /** | |
| 77 * Constructs an exception that wraps {@code cause} thrown by a {@link UrlRe quest.Callback}. | |
| 78 * | |
| 79 * @param message explanation of failure. | |
| 80 * @param cause exception thrown by {@link UrlRequestListener} that's being wrapped. | |
| 81 */ | |
| 82 public UrlRequestException(String message, Throwable cause) { | |
|
xunjieli
2015/11/06 15:23:22
Suggest moving this constructor after UrlRequestEx
pauljensen
2016/01/25 01:43:50
Done.
| |
| 18 super(message, cause); | 83 super(message, cause); |
| 19 mNetError = 0; | 84 mErrorCode = ERROR_LISTENER_THREW; |
| 85 mCronetInternalErrorCode = 0; | |
| 20 } | 86 } |
| 21 | 87 |
| 22 UrlRequestException(String message, int netError) { | 88 /** |
| 89 * Constructs an exception with a specific error. | |
| 90 * | |
| 91 * @param message explanation of failure. | |
| 92 * @param errorCode error code, one of {@link #ERROR_LISTENER_THREW ERROR_*} . | |
| 93 * @param cronetInternalErrorCode Cronet internal error code, one of | |
| 94 * <a href=https://chromium.googlesource.com/chromium/src/+/master/net/base/ net_error_list.h> | |
| 95 * these</a>. | |
| 96 */ | |
| 97 public UrlRequestException(String message, int errorCode, int cronetInternal ErrorCode) { | |
|
xunjieli
2015/11/06 15:23:22
Why do we change the constructors from package pro
pauljensen
2016/01/25 01:43:50
This was suggested by API review to allows users t
xunjieli
2016/01/25 14:37:02
Acknowledged.
| |
| 23 super(message, null); | 98 super(message, null); |
| 24 mNetError = netError; | 99 mErrorCode = errorCode; |
| 100 mCronetInternalErrorCode = cronetInternalErrorCode; | |
| 25 } | 101 } |
| 26 | 102 |
| 27 /** @return Error code if exception is reported by native. */ | 103 /** |
| 28 public int netError() { | 104 * Returns error code, one of {@link #ERROR_LISTENER_THREW ERROR_*}. |
| 29 return mNetError; | 105 * |
| 106 * @return error code, one of {@link #ERROR_LISTENER_THREW ERROR_*}. | |
| 107 */ | |
| 108 public int getErrorCode() { | |
| 109 return mErrorCode; | |
| 110 } | |
| 111 | |
| 112 /** | |
| 113 * Returns a Cronet internal error code. This may provide more specific erro r | |
| 114 * diagnosis than {@link #getErrorCode}, but the constant values are not exp osed to Java and | |
| 115 * may change over time. See | |
| 116 * <a href=https://chromium.googlesource.com/chromium/src/+/master/net/base/ net_error_list.h> | |
| 117 * here</a> for the lastest list of values. | |
| 118 * | |
| 119 * @return Cronet internal error code. | |
| 120 */ | |
| 121 public int getCronetInternalErrorCode() { | |
| 122 return mCronetInternalErrorCode; | |
| 123 } | |
| 124 | |
| 125 /** | |
| 126 * Returns {@code true} if retrying this request right away might succeed, { @code false} | |
| 127 * otherwise. For example returns {@code true} when {@link #getErrorCode} re turns | |
| 128 * {@link #ERROR_NETWORK_CHANGED} because trying the request might succeed u sing the new | |
| 129 * network configuration, but {@code false} when {@code getErrorCode()} retu rns | |
| 130 * {@link #ERROR_INTERNET_DISCONNECTED} because retrying the request right a way will | |
| 131 * encounter the same failure (instead retrying should be delayed until devi ce regains | |
| 132 * network connectivity). Returns {@code false} when {@code getErrorCode()} returns | |
| 133 * {@link #ERROR_LISTENER_THREW}. | |
| 134 * | |
| 135 * @return {@code true} if retrying this request right away might succeed, { @code false} | |
| 136 * otherwise. | |
| 137 */ | |
| 138 public boolean immediatelyRetryable() { | |
| 139 switch (mErrorCode) { | |
| 140 case ERROR_LISTENER_THREW: | |
| 141 case ERROR_HOSTNAME_NOT_RESOLVED: | |
| 142 case ERROR_INTERNET_DISCONNECTED: | |
| 143 case ERROR_CONNECTION_REFUSED: | |
| 144 case ERROR_ADDRESS_UNREACHABLE: | |
| 145 case ERROR_OTHER: | |
| 146 default: | |
| 147 return false; | |
| 148 case ERROR_NETWORK_CHANGED: | |
| 149 case ERROR_TIMED_OUT: | |
| 150 case ERROR_CONNECTION_CLOSED: | |
| 151 case ERROR_CONNECTION_TIMED_OUT: | |
| 152 case ERROR_CONNECTION_RESET: | |
| 153 return true; | |
| 154 } | |
| 30 } | 155 } |
| 31 } | 156 } |
| OLD | NEW |