1 /*
2  * Copyright (C) 2022 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.net.ipsec.ike.exceptions;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SuppressLint;
21 import android.net.ipsec.ike.ChildSessionCallback;
22 import android.net.ipsec.ike.IkeSessionCallback;
23 
24 import com.android.internal.net.ipsec.ike.utils.IkeMetrics;
25 
26 import java.io.IOException;
27 import java.net.UnknownHostException;
28 import java.util.Objects;
29 
30 /** Wrapper for I/O exceptions encountered during IKE operations. */
31 // Does not make sense to name it IkeIoException since "I" and "O" represent two words. The current
32 // name follows the convention set by Java.
33 @SuppressLint("AcronymName")
34 public final class IkeIOException extends IkeNonProtocolException {
35     /**
36      * Constructs a new exception with the specified cause.
37      *
38      * <p>Callers are not generally expected to instantiate this object themselves, except for
39      * testing. A reference is passed via {@link IkeSessionCallback} or {@link
40      * ChildSessionCallback}.
41      *
42      * @param cause the cause (which is saved for later retrieval by the {@link #getCause()}
43      *     method).
44      */
IkeIOException(@onNull IOException cause)45     public IkeIOException(@NonNull IOException cause) {
46         super(Objects.requireNonNull(cause, "cause MUST NOT be null"));
47     }
48 
49     /**
50      * Returns the cause of this IkeIOException.
51      *
52      * @return the cause of this IkeIOException. It might be a subclass of IOException that
53      *     represents a specific type of I/O issue. For example, {@link UnknownHostException} and
54      *     {@link IkeTimeoutException}.
55      */
56     @Override
57     @NonNull
getCause()58     public IOException getCause() {
59         return (IOException) super.getCause();
60     }
61 
62     /** @hide */
63     @Override
initCause(Throwable cause)64     public synchronized Throwable initCause(Throwable cause) {
65         throw new UnsupportedOperationException("It is not allowed to set cause with this method");
66     }
67 
68     /**
69      * Returns the error code for metrics
70      *
71      * @hide
72      */
73     @Override
getMetricsErrorCode()74     public int getMetricsErrorCode() {
75         if (getCause() instanceof UnknownHostException) {
76             return IkeMetrics.IKE_ERROR_IO_DNS_FAILURE;
77         } else if (getCause() instanceof IkeTimeoutException) {
78             return IkeMetrics.IKE_ERROR_IO_TIMEOUT;
79         } else {
80             return IkeMetrics.IKE_ERROR_IO_GENERAL;
81         }
82     }
83 }
84