Home | History | Annotate | Download | only in io
      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 libcore.io;
     18 
     19 import java.net.UnknownHostException;
     20 import libcore.io.OsConstants;
     21 
     22 /**
     23  * An unchecked exception thrown when the {@link Os} {@code getaddrinfo} or {@code getnameinfo}
     24  * methods fail. This exception contains the native error value, for comparison against the
     25  * {@code GAI_} constants in {@link OsConstants}, should sophisticated
     26  * callers need to adjust their behavior based on the exact failure.
     27  */
     28 public final class GaiException extends RuntimeException {
     29     private final String functionName;
     30     public final int error;
     31 
     32     public GaiException(String functionName, int error) {
     33         this.functionName = functionName;
     34         this.error = error;
     35     }
     36 
     37     public GaiException(String functionName, int error, Throwable cause) {
     38         super(cause);
     39         this.functionName = functionName;
     40         this.error = error;
     41     }
     42 
     43     /**
     44      * Converts the stashed function name and error value to a human-readable string.
     45      * We do this here rather than in the constructor so that callers only pay for
     46      * this if they need it.
     47      */
     48     @Override public String getMessage() {
     49         String gaiName = OsConstants.gaiName(error);
     50         if (gaiName == null) {
     51             gaiName = "GAI_ error " + error;
     52         }
     53         String description = Libcore.os.gai_strerror(error);
     54         return functionName + " failed: " + gaiName + " (" + description + ")";
     55     }
     56 
     57     public UnknownHostException rethrowAsUnknownHostException(String detailMessage) throws UnknownHostException {
     58         UnknownHostException newException = new UnknownHostException(detailMessage);
     59         newException.initCause(this);
     60         throw newException;
     61     }
     62 
     63     public UnknownHostException rethrowAsUnknownHostException() throws UnknownHostException {
     64         throw rethrowAsUnknownHostException(getMessage());
     65     }
     66 }
     67