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 UrlRequest.Callback#onFailed UrlRequest.Callback.o nFailed()} when: |
| 11 * which case netError() will contain native error code. | 11 * <ul> |
| 12 * <li>{@link UrlRequest.Callback} or {@link UploadDataProvider} method throws a n 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_EXCEPTION_THROWN }. | |
| 15 * <li>Cronet fails to process a network request. In this case | |
| 16 * {@link #getErrorCode} and {@link #getCronetInternalErrorCode} can be used to get more | |
| 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 equest.Callback} or |
| 23 * {@link UploadDataProvider}. Wrapped exception can be retrieved using | |
| 24 * {@link IOException#getCause}. | |
| 25 */ | |
| 26 public static final int ERROR_LISTENER_EXCEPTION_THROWN = 0; | |
| 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 = 1; | |
| 31 /** | |
| 32 * Error code indicating the device was not connected to any network. | |
| 33 */ | |
| 34 public static final int ERROR_INTERNET_DISCONNECTED = 2; | |
| 35 /** | |
| 36 * Error code indicating that as the request was processed the network confi guration changed. | |
| 37 */ | |
| 38 public static final int ERROR_NETWORK_CHANGED = 3; | |
| 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 = 4; | |
| 44 /** | |
| 45 * Error code indicating the connection was closed unexpectedly. | |
| 46 */ | |
| 47 public static final int ERROR_CONNECTION_CLOSED = 5; | |
| 48 /** | |
| 49 * Error code indicating the connection attempt timed out. | |
| 50 */ | |
| 51 public static final int ERROR_CONNECTION_TIMED_OUT = 6; | |
| 52 /** | |
| 53 * Error code indicating the connection attempt was refused. | |
| 54 */ | |
| 55 public static final int ERROR_CONNECTION_REFUSED = 7; | |
| 56 /** | |
| 57 * Error code indicating the connection was unexpectedly reset. | |
| 58 */ | |
| 59 public static final int ERROR_CONNECTION_RESET = 8; | |
| 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 = 9; | |
| 65 /** | |
| 66 * Error code indicating another type of error was encountered. | |
| 67 * {@link #getCronetInternalErrorCode} can be consulted to get a more specif ic cause. | |
| 68 */ | |
| 69 public static final int ERROR_OTHER = 10; | |
| 16 | 70 |
| 17 UrlRequestException(String message, Throwable cause) { | 71 // Error code, one of ERROR_* |
| 18 super(message, cause); | 72 private final int mErrorCode; |
| 19 mNetError = 0; | 73 // Cronet internal error code. |
| 74 private final int mCronetInternalErrorCode; | |
| 75 | |
| 76 /** | |
| 77 * Constructs an exception with a specific error. | |
| 78 * | |
| 79 * @param message explanation of failure. | |
| 80 * @param errorCode error code, one of {@link #ERROR_LISTENER_EXCEPTION_THRO WN ERROR_*}. | |
| 81 * @param cronetInternalErrorCode Cronet internal error code, one of | |
| 82 * <a href=https://chromium.googlesource.com/chromium/src/+/master/net/base/ net_error_list.h> | |
| 83 * these</a>. | |
| 84 */ | |
| 85 public UrlRequestException(String message, int errorCode, int cronetInternal ErrorCode) { | |
| 86 super(message, null); | |
| 87 // ERROR_* values cannot be directly initialized to UrlRequestError valu es because javadoc | |
| 88 // generation will fail because it is not passed the generated java cons tants. | |
|
xunjieli
2016/01/25 14:37:02
Are we able to pass the generated java file to jav
pauljensen
2016/02/02 18:45:45
Done.
| |
| 89 switch (errorCode) { | |
| 90 case UrlRequestError.LISTENER_EXCEPTION_THROWN: | |
| 91 mErrorCode = ERROR_LISTENER_EXCEPTION_THROWN; | |
| 92 break; | |
| 93 case UrlRequestError.HOSTNAME_NOT_RESOLVED: | |
| 94 mErrorCode = ERROR_HOSTNAME_NOT_RESOLVED; | |
| 95 break; | |
| 96 case UrlRequestError.INTERNET_DISCONNECTED: | |
| 97 mErrorCode = ERROR_INTERNET_DISCONNECTED; | |
| 98 break; | |
| 99 case UrlRequestError.NETWORK_CHANGED: | |
| 100 mErrorCode = ERROR_NETWORK_CHANGED; | |
| 101 break; | |
| 102 case UrlRequestError.TIMED_OUT: | |
| 103 mErrorCode = ERROR_TIMED_OUT; | |
| 104 break; | |
| 105 case UrlRequestError.CONNECTION_CLOSED: | |
| 106 mErrorCode = ERROR_CONNECTION_CLOSED; | |
| 107 break; | |
| 108 case UrlRequestError.CONNECTION_TIMED_OUT: | |
| 109 mErrorCode = ERROR_CONNECTION_TIMED_OUT; | |
| 110 break; | |
| 111 case UrlRequestError.CONNECTION_REFUSED: | |
| 112 mErrorCode = ERROR_CONNECTION_REFUSED; | |
| 113 break; | |
| 114 case UrlRequestError.CONNECTION_RESET: | |
| 115 mErrorCode = ERROR_CONNECTION_RESET; | |
| 116 break; | |
| 117 case UrlRequestError.ADDRESS_UNREACHABLE: | |
| 118 mErrorCode = ERROR_ADDRESS_UNREACHABLE; | |
| 119 break; | |
| 120 case UrlRequestError.OTHER: | |
| 121 mErrorCode = ERROR_OTHER; | |
| 122 break; | |
| 123 default: | |
| 124 throw new IllegalArgumentException("Unknown error code " + error Code); | |
| 125 } | |
| 126 mCronetInternalErrorCode = cronetInternalErrorCode; | |
| 20 } | 127 } |
| 21 | 128 |
| 22 UrlRequestException(String message, int netError) { | 129 /** |
| 23 super(message, null); | 130 * Constructs an exception that wraps {@code cause} thrown by a {@link UrlRe quest.Callback}. |
| 24 mNetError = netError; | 131 * |
| 132 * @param message explanation of failure. | |
| 133 * @param cause exception thrown by {@link UrlRequest.Callback} that's being wrapped. | |
| 134 */ | |
| 135 public UrlRequestException(String message, Throwable cause) { | |
| 136 super(message, cause); | |
| 137 mErrorCode = ERROR_LISTENER_EXCEPTION_THROWN; | |
| 138 mCronetInternalErrorCode = 0; | |
| 25 } | 139 } |
| 26 | 140 |
| 27 /** @return Error code if exception is reported by native. */ | 141 /** |
| 28 public int netError() { | 142 * Returns error code, one of {@link #ERROR_LISTENER_EXCEPTION_THROWN ERROR_ *}. |
| 29 return mNetError; | 143 * |
| 144 * @return error code, one of {@link #ERROR_LISTENER_EXCEPTION_THROWN ERROR_ *}. | |
| 145 */ | |
| 146 public int getErrorCode() { | |
| 147 return mErrorCode; | |
| 148 } | |
| 149 | |
| 150 /** | |
| 151 * Returns a Cronet internal error code. This may provide more specific erro r | |
| 152 * diagnosis than {@link #getErrorCode}, but the constant values are not exp osed to Java and | |
| 153 * may change over time. See | |
| 154 * <a href=https://chromium.googlesource.com/chromium/src/+/master/net/base/ net_error_list.h> | |
| 155 * here</a> for the lastest list of values. | |
| 156 * | |
| 157 * @return Cronet internal error code. | |
| 158 */ | |
| 159 public int getCronetInternalErrorCode() { | |
| 160 return mCronetInternalErrorCode; | |
| 161 } | |
| 162 | |
| 163 /** | |
| 164 * Returns {@code true} if retrying this request right away might succeed, { @code false} | |
| 165 * otherwise. For example returns {@code true} when {@link #getErrorCode} re turns | |
| 166 * {@link #ERROR_NETWORK_CHANGED} because trying the request might succeed u sing the new | |
| 167 * network configuration, but {@code false} when {@code getErrorCode()} retu rns | |
| 168 * {@link #ERROR_INTERNET_DISCONNECTED} because retrying the request right a way will | |
| 169 * encounter the same failure (instead retrying should be delayed until devi ce regains | |
| 170 * network connectivity). Returns {@code false} when {@code getErrorCode()} returns | |
| 171 * {@link #ERROR_LISTENER_EXCEPTION_THROWN}. | |
| 172 * | |
| 173 * @return {@code true} if retrying this request right away might succeed, { @code false} | |
| 174 * otherwise. | |
| 175 */ | |
| 176 public boolean immediatelyRetryable() { | |
| 177 switch (mErrorCode) { | |
| 178 case ERROR_LISTENER_EXCEPTION_THROWN: | |
| 179 case ERROR_HOSTNAME_NOT_RESOLVED: | |
| 180 case ERROR_INTERNET_DISCONNECTED: | |
| 181 case ERROR_CONNECTION_REFUSED: | |
| 182 case ERROR_ADDRESS_UNREACHABLE: | |
| 183 case ERROR_OTHER: | |
| 184 default: | |
| 185 return false; | |
| 186 case ERROR_NETWORK_CHANGED: | |
| 187 case ERROR_TIMED_OUT: | |
| 188 case ERROR_CONNECTION_CLOSED: | |
| 189 case ERROR_CONNECTION_TIMED_OUT: | |
| 190 case ERROR_CONNECTION_RESET: | |
| 191 return true; | |
| 192 } | |
| 30 } | 193 } |
| 31 } | 194 } |
| OLD | NEW |