Chromium Code Reviews| Index: components/cronet/android/api/src/org/chromium/net/UrlRequestException.java |
| diff --git a/components/cronet/android/api/src/org/chromium/net/UrlRequestException.java b/components/cronet/android/api/src/org/chromium/net/UrlRequestException.java |
| index 3557324c1e7b6d5d716131b5cf30fdafb215360f..2f428ed8106194e55b9781abdea848e32030fa70 100644 |
| --- a/components/cronet/android/api/src/org/chromium/net/UrlRequestException.java |
| +++ b/components/cronet/android/api/src/org/chromium/net/UrlRequestException.java |
| @@ -7,25 +7,150 @@ package org.chromium.net; |
| import java.io.IOException; |
| /** |
| - * Exception after UrlRequest start. Could be reported by network stack, in |
| - * which case netError() will contain native error code. |
| + * Exception passed to {@link UrlRequestListener#onFailed UrlRequestListener.onFailed()} when: |
| + * <ul> |
| + * <li>{@link UrlRequestListener} or {@link UploadDataProvider} method throws an exception. In this |
| + * case {@link IOException#getCause getCause()} can be used to find the thrown exception. |
| + * {@link #getErrorCode} will return {@link #ERROR_LISTENER_THREW}. |
| + * <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
|
| + * {@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.
|
| + * information about the specific type of failure. |
| + * </ul> |
| */ |
| public class UrlRequestException extends IOException { |
| - /** Net error code if exception is reported by native. */ |
| - final int mNetError; |
| + /** |
| + * Error code indicating this class wraps an exception thrown by {@link UrlRequestListener} or |
| + * {@link UploadDataProvider}. Wrapped exception can be retrieved using |
| + * {@link IOException#getCause}. |
| + */ |
| + public static final int ERROR_LISTENER_THREW = UrlRequestError.LISTENER_THREW; |
| + /** |
| + * Error code indicating the host being sent the request could not be resolved to an IP address. |
| + */ |
| + public static final int ERROR_HOSTNAME_NOT_RESOLVED = UrlRequestError.HOSTNAME_NOT_RESOLVED; |
| + /** |
| + * Error code indicating the device was not connected to any network. |
| + */ |
| + public static final int ERROR_INTERNET_DISCONNECTED = UrlRequestError.INTERNET_DISCONNECTED; |
| + /** |
| + * Error code indicating that as the request was processed the network configuration chnaged. |
|
xunjieli
2015/11/06 15:23:22
nit: typo in "changed"
pauljensen
2016/01/25 01:43:50
Done.
|
| + */ |
| + public static final int ERROR_NETWORK_CHANGED = UrlRequestError.NETWORK_CHANGED; |
| + /** |
| + * Error code indicating a timeout expired. Timeouts expiring while attempting to connect will |
| + * be reported as the more specific {@link #ERROR_CONNECTION_TIMED_OUT}. |
| + */ |
| + public static final int ERROR_TIMED_OUT = UrlRequestError.TIMED_OUT; |
| + /** |
| + * Error code indicating the connection was closed unexpectedly. |
| + */ |
| + public static final int ERROR_CONNECTION_CLOSED = UrlRequestError.CONNECTION_CLOSED; |
| + /** |
| + * Error code indicating the connection attempt timed out. |
| + */ |
| + public static final int ERROR_CONNECTION_TIMED_OUT = UrlRequestError.CONNECTION_TIMED_OUT; |
| + /** |
| + * Error code indicating the connection attempt was refused. |
| + */ |
| + public static final int ERROR_CONNECTION_REFUSED = UrlRequestError.CONNECTION_REFUSED; |
| + /** |
| + * Error code indicating the connection was unexpectedly reset. |
| + */ |
| + public static final int ERROR_CONNECTION_RESET = UrlRequestError.CONNECTION_RESET; |
| + /** |
| + * Error code indicating the IP address being contacted is unreachable, meaning there is no |
| + * route to the specified host or network. |
| + */ |
| + public static final int ERROR_ADDRESS_UNREACHABLE = UrlRequestError.ADDRESS_UNREACHABLE; |
| + /** |
| + * Error code indicating another type of error was encountered. {@link #netError} can be |
|
xunjieli
2015/11/06 15:23:22
nit: netError -> getCronetInternalErrorCode?
pauljensen
2016/01/25 01:43:50
Done.
|
| + * consulted to get a more specific cause. |
| + */ |
| + public static final int ERROR_OTHER = UrlRequestError.OTHER; |
| - UrlRequestException(String message, Throwable cause) { |
| + // Error code, one of ERROR_* |
| + private final int mErrorCode; |
| + // Cronet internal error code. |
| + private final int mCronetInternalErrorCode; |
| + |
| + /** |
| + * Constructs an exception that wraps {@code cause} thrown by a {@link UrlRequest.Callback}. |
| + * |
| + * @param message explanation of failure. |
| + * @param cause exception thrown by {@link UrlRequestListener} that's being wrapped. |
| + */ |
| + 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.
|
| super(message, cause); |
| - mNetError = 0; |
| + mErrorCode = ERROR_LISTENER_THREW; |
| + mCronetInternalErrorCode = 0; |
| } |
| - UrlRequestException(String message, int netError) { |
| + /** |
| + * Constructs an exception with a specific error. |
| + * |
| + * @param message explanation of failure. |
| + * @param errorCode error code, one of {@link #ERROR_LISTENER_THREW ERROR_*}. |
| + * @param cronetInternalErrorCode Cronet internal error code, one of |
| + * <a href=https://chromium.googlesource.com/chromium/src/+/master/net/base/net_error_list.h> |
| + * these</a>. |
| + */ |
| + public UrlRequestException(String message, int errorCode, int cronetInternalErrorCode) { |
|
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.
|
| super(message, null); |
| - mNetError = netError; |
| + mErrorCode = errorCode; |
| + mCronetInternalErrorCode = cronetInternalErrorCode; |
| + } |
| + |
| + /** |
| + * Returns error code, one of {@link #ERROR_LISTENER_THREW ERROR_*}. |
| + * |
| + * @return error code, one of {@link #ERROR_LISTENER_THREW ERROR_*}. |
| + */ |
| + public int getErrorCode() { |
| + return mErrorCode; |
| + } |
| + |
| + /** |
| + * Returns a Cronet internal error code. This may provide more specific error |
| + * diagnosis than {@link #getErrorCode}, but the constant values are not exposed to Java and |
| + * may change over time. See |
| + * <a href=https://chromium.googlesource.com/chromium/src/+/master/net/base/net_error_list.h> |
| + * here</a> for the lastest list of values. |
| + * |
| + * @return Cronet internal error code. |
| + */ |
| + public int getCronetInternalErrorCode() { |
| + return mCronetInternalErrorCode; |
| } |
| - /** @return Error code if exception is reported by native. */ |
| - public int netError() { |
| - return mNetError; |
| + /** |
| + * Returns {@code true} if retrying this request right away might succeed, {@code false} |
| + * otherwise. For example returns {@code true} when {@link #getErrorCode} returns |
| + * {@link #ERROR_NETWORK_CHANGED} because trying the request might succeed using the new |
| + * network configuration, but {@code false} when {@code getErrorCode()} returns |
| + * {@link #ERROR_INTERNET_DISCONNECTED} because retrying the request right away will |
| + * encounter the same failure (instead retrying should be delayed until device regains |
| + * network connectivity). Returns {@code false} when {@code getErrorCode()} returns |
| + * {@link #ERROR_LISTENER_THREW}. |
| + * |
| + * @return {@code true} if retrying this request right away might succeed, {@code false} |
| + * otherwise. |
| + */ |
| + public boolean immediatelyRetryable() { |
| + switch (mErrorCode) { |
| + case ERROR_LISTENER_THREW: |
| + case ERROR_HOSTNAME_NOT_RESOLVED: |
| + case ERROR_INTERNET_DISCONNECTED: |
| + case ERROR_CONNECTION_REFUSED: |
| + case ERROR_ADDRESS_UNREACHABLE: |
| + case ERROR_OTHER: |
| + default: |
| + return false; |
| + case ERROR_NETWORK_CHANGED: |
| + case ERROR_TIMED_OUT: |
| + case ERROR_CONNECTION_CLOSED: |
| + case ERROR_CONNECTION_TIMED_OUT: |
| + case ERROR_CONNECTION_RESET: |
| + return true; |
| + } |
| } |
| } |