1 // Copyright 2016 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.net; 6 7 /** 8 * Exception passed to {@link UrlRequest.Callback#onFailed UrlRequest.Callback.onFailed()} when 9 * Cronet fails to process a network request. In this case {@link #getErrorCode} and {@link 10 * #getCronetInternalErrorCode} can be used to get more information about the specific type of 11 * failure. If {@link #getErrorCode} returns {@link #ERROR_QUIC_PROTOCOL_FAILED}, this exception can 12 * be cast to a {@link QuicException} which can provide further details. 13 */ 14 public abstract class NetworkException extends CronetException { 15 /** Error code indicating the host being sent the request could not be resolved to an IP address. */ 16 public static final int ERROR_HOSTNAME_NOT_RESOLVED = 1; 17 18 /** Error code indicating the device was not connected to any network. */ 19 public static final int ERROR_INTERNET_DISCONNECTED = 2; 20 21 /** 22 * Error code indicating that as the request was processed the network configuration changed. 23 * When 24 * {@link #getErrorCode} returns this code, this exception may be cast to {@link QuicException} 25 * for more information if <a href="https://www.chromium.org/quic"> QUIC</a> protocol is used. 26 */ 27 public static final int ERROR_NETWORK_CHANGED = 3; 28 29 /** 30 * Error code indicating a timeout expired. Timeouts expiring while attempting to connect will 31 * be reported as the more specific {@link #ERROR_CONNECTION_TIMED_OUT}. 32 */ 33 public static final int ERROR_TIMED_OUT = 4; 34 35 /** Error code indicating the connection was closed unexpectedly. */ 36 public static final int ERROR_CONNECTION_CLOSED = 5; 37 38 /** Error code indicating the connection attempt timed out. */ 39 public static final int ERROR_CONNECTION_TIMED_OUT = 6; 40 41 /** Error code indicating the connection attempt was refused. */ 42 public static final int ERROR_CONNECTION_REFUSED = 7; 43 44 /** Error code indicating the connection was unexpectedly reset. */ 45 public static final int ERROR_CONNECTION_RESET = 8; 46 47 /** 48 * Error code indicating the IP address being contacted is unreachable, meaning there is no 49 * route to the specified host or network. 50 */ 51 public static final int ERROR_ADDRESS_UNREACHABLE = 9; 52 53 /** 54 * Error code indicating an error related to the <a href="https://www.chromium.org/quic"> 55 * QUIC</a> protocol. When {@link #getErrorCode} returns this code, this exception can be cast 56 * to {@link QuicException} for more information. 57 */ 58 public static final int ERROR_QUIC_PROTOCOL_FAILED = 10; 59 60 /** 61 * Error code indicating another type of error was encountered. {@link 62 * #getCronetInternalErrorCode} can be consulted to get a more specific cause. 63 */ 64 public static final int ERROR_OTHER = 11; 65 66 /** 67 * Constructs an exception that is caused by a network error. 68 * 69 * @param message explanation of failure. 70 * @param cause the cause (which is saved for later retrieval by the {@link 71 * java.io.IOException#getCause getCause()} method). A null value is permitted, and indicates 72 * that the cause is nonexistent or unknown. 73 */ NetworkException(String message, Throwable cause)74 protected NetworkException(String message, Throwable cause) { 75 super(message, cause); 76 } 77 78 /** 79 * Returns error code, one of {@link #ERROR_HOSTNAME_NOT_RESOLVED ERROR_*}. 80 * 81 * @return error code, one of {@link #ERROR_HOSTNAME_NOT_RESOLVED ERROR_*}. 82 */ getErrorCode()83 public abstract int getErrorCode(); 84 85 /** 86 * Returns a Cronet internal error code. This may provide more specific error diagnosis than 87 * {@link #getErrorCode}, but the constant values are not exposed to Java and may change over 88 * time. See 89 * <a href=https://chromium.googlesource.com/chromium/src/+/main/net/base/net_error_list.h> 90 * here</a> for the lastest list of values. 91 * 92 * @return Cronet internal error code. 93 */ getCronetInternalErrorCode()94 public abstract int getCronetInternalErrorCode(); 95 96 /** 97 * Returns {@code true} if retrying this request right away might succeed, {@code false} 98 * otherwise. For example returns {@code true} when {@link #getErrorCode} returns {@link 99 * #ERROR_NETWORK_CHANGED} because trying the request might succeed using the new network 100 * configuration, but {@code false} when {@code getErrorCode()} returns {@link 101 * #ERROR_INTERNET_DISCONNECTED} because retrying the request right away will encounter the same 102 * failure (instead retrying should be delayed until device regains network connectivity). 103 * 104 * @return {@code true} if retrying this request right away might succeed, {@code false} 105 * otherwise. 106 */ immediatelyRetryable()107 public abstract boolean immediatelyRetryable(); 108 } 109