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