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.impl; 6 7 import android.net.http.NetworkException; 8 9 /** 10 * Implements {@link NetworkException}. 11 */ 12 public class NetworkExceptionImpl extends NetworkException { 13 // Error code, one of ERROR_* 14 protected final int mErrorCode; 15 // Cronet internal error code. 16 protected final int mCronetInternalErrorCode; 17 18 /** 19 * Constructs an exception with a specific error. 20 * 21 * @param message explanation of failure. 22 * @param errorCode error code, one of {@link #ERROR_HOSTNAME_NOT_RESOLVED ERROR_*}. 23 * @param cronetInternalErrorCode Cronet internal error code, one of 24 * <a href=https://chromium.googlesource.com/chromium/src/+/main/net/base/net_error_list.h> 25 * these</a>. 26 */ NetworkExceptionImpl(String message, int errorCode, int cronetInternalErrorCode)27 public NetworkExceptionImpl(String message, int errorCode, int cronetInternalErrorCode) { 28 super(message, null); 29 assert errorCode > 0 && errorCode < 12; 30 assert cronetInternalErrorCode < 0; 31 mErrorCode = errorCode; 32 mCronetInternalErrorCode = cronetInternalErrorCode; 33 } 34 35 @Override 36 public int getErrorCode() { 37 return mErrorCode; 38 } 39 40 @Override 41 public int getInternalErrorCode() { 42 return mCronetInternalErrorCode; 43 } 44 45 @Override 46 public boolean isImmediatelyRetryable() { 47 switch (mErrorCode) { 48 case ERROR_HOSTNAME_NOT_RESOLVED: 49 case ERROR_INTERNET_DISCONNECTED: 50 case ERROR_CONNECTION_REFUSED: 51 case ERROR_ADDRESS_UNREACHABLE: 52 case ERROR_OTHER: 53 default: 54 return false; 55 case ERROR_NETWORK_CHANGED: 56 case ERROR_TIMED_OUT: 57 case ERROR_CONNECTION_CLOSED: 58 case ERROR_CONNECTION_TIMED_OUT: 59 case ERROR_CONNECTION_RESET: 60 return true; 61 } 62 } 63 64 @Override 65 public String getMessage() { 66 StringBuilder b = new StringBuilder(super.getMessage()); 67 b.append(", ErrorCode=").append(mErrorCode); 68 if (mCronetInternalErrorCode != 0) { 69 b.append(", InternalErrorCode=").append(mCronetInternalErrorCode); 70 } 71 b.append(", Retryable=").append(isImmediatelyRetryable()); 72 return b.toString(); 73 } 74 } 75