1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.system;
18 
19 import java.net.UnknownHostException;
20 import libcore.io.Libcore;
21 
22 /**
23  * An unchecked exception thrown when {@code getaddrinfo} or {@code getnameinfo} fails.
24  * This exception contains the native {@link #error} value, should sophisticated
25  * callers need to adjust their behavior based on the exact failure.
26  *
27  * @hide
28  */
29 public final class GaiException extends RuntimeException {
30   private final String functionName;
31 
32   /**
33    * The native error value, for comparison with the {@code GAI_} constants in {@link OsConstants}.
34    */
35   public final int error;
36 
37   /**
38    * Constructs an instance with the given function name and error value.
39    */
GaiException(String functionName, int error)40   public GaiException(String functionName, int error) {
41     this.functionName = functionName;
42     this.error = error;
43   }
44 
45   /**
46    * Constructs an instance with the given function name, error value, and cause.
47    */
GaiException(String functionName, int error, Throwable cause)48   public GaiException(String functionName, int error, Throwable cause) {
49     super(cause);
50     this.functionName = functionName;
51     this.error = error;
52   }
53 
54   /**
55    * Converts the stashed function name and error value to a human-readable string.
56    * We do this here rather than in the constructor so that callers only pay for
57    * this if they need it.
58    */
getMessage()59   @Override public String getMessage() {
60     String gaiName = OsConstants.gaiName(error);
61     if (gaiName == null) {
62       gaiName = "GAI_ error " + error;
63     }
64     String description = Libcore.os.gai_strerror(error);
65     return functionName + " failed: " + gaiName + " (" + description + ")";
66   }
67 
68   /**
69    * @hide - internal use only.
70    */
rethrowAsUnknownHostException(String detailMessage)71   public UnknownHostException rethrowAsUnknownHostException(String detailMessage) throws UnknownHostException {
72     UnknownHostException newException = new UnknownHostException(detailMessage);
73     newException.initCause(this);
74     throw newException;
75   }
76 
77   /**
78    * @hide - internal use only.
79    */
rethrowAsUnknownHostException()80   public UnknownHostException rethrowAsUnknownHostException() throws UnknownHostException {
81     throw rethrowAsUnknownHostException(getMessage());
82   }
83 }
84