blob: 7de3dd13769e20e28e1824bc76eafc31c14a7b7d [file] [log] [blame]
Daniel Brightf9e945b2020-06-15 16:10:01 -07001/*
2 * Copyright (C) 2020 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
17package android.net;
18
19import android.annotation.IntDef;
20import android.annotation.NonNull;
21import android.annotation.SystemApi;
22import android.util.Log;
23
sewookseoe7c48142022-03-10 03:06:43 +000024import com.android.internal.annotations.VisibleForTesting;
25
Daniel Brightf9e945b2020-06-15 16:10:01 -070026import java.lang.annotation.Retention;
27import java.lang.annotation.RetentionPolicy;
28
29/**
30 * This is the exception type passed back through the onError method on {@link QosCallback}.
31 * {@link QosCallbackException#getCause()} contains the actual error that caused this exception.
32 *
33 * The possible exception types as causes are:
34 * 1. {@link NetworkReleasedException}
35 * 2. {@link SocketNotBoundException}
36 * 3. {@link UnsupportedOperationException}
37 * 4. {@link SocketLocalAddressChangedException}
38 *
39 * @hide
40 */
41@SystemApi
42public final class QosCallbackException extends Exception {
43
44 /** @hide */
45 @IntDef(prefix = {"EX_TYPE_"}, value = {
46 EX_TYPE_FILTER_NONE,
47 EX_TYPE_FILTER_NETWORK_RELEASED,
48 EX_TYPE_FILTER_SOCKET_NOT_BOUND,
sewookseoafc22b02022-03-07 11:03:13 +000049 EX_TYPE_FILTER_SOCKET_NOT_CONNECTED,
Daniel Brightf9e945b2020-06-15 16:10:01 -070050 EX_TYPE_FILTER_NOT_SUPPORTED,
51 EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED,
sewookseoafc22b02022-03-07 11:03:13 +000052 EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED,
Daniel Brightf9e945b2020-06-15 16:10:01 -070053 })
54 @Retention(RetentionPolicy.SOURCE)
55 public @interface ExceptionType {}
56
57 private static final String TAG = "QosCallbackException";
58
59 // Types of exceptions supported //
Remi NGUYEN VANbd7a4852022-07-13 20:42:13 +090060 // The constants are used for the sendQosCallbackError system API, so they must not be changed
61 // as there may be callers relying on their historical values to call that API.
62 // TODO: mark the constants as @SystemApi, since they are necessary to call a system API.
Daniel Brightf9e945b2020-06-15 16:10:01 -070063 /** {@hide} */
64 public static final int EX_TYPE_FILTER_NONE = 0;
65
66 /** {@hide} */
67 public static final int EX_TYPE_FILTER_NETWORK_RELEASED = 1;
68
69 /** {@hide} */
70 public static final int EX_TYPE_FILTER_SOCKET_NOT_BOUND = 2;
71
72 /** {@hide} */
Remi NGUYEN VANbd7a4852022-07-13 20:42:13 +090073 public static final int EX_TYPE_FILTER_NOT_SUPPORTED = 3;
Daniel Brightf9e945b2020-06-15 16:10:01 -070074
75 /** {@hide} */
Remi NGUYEN VANbd7a4852022-07-13 20:42:13 +090076 public static final int EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED = 4;
sewookseoafc22b02022-03-07 11:03:13 +000077
78 /** {@hide} */
Remi NGUYEN VANbd7a4852022-07-13 20:42:13 +090079 public static final int EX_TYPE_FILTER_SOCKET_NOT_CONNECTED = 5;
sewookseoafc22b02022-03-07 11:03:13 +000080
81 /** {@hide} */
82 public static final int EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED = 6;
Daniel Brightf9e945b2020-06-15 16:10:01 -070083
84 /**
85 * Creates exception based off of a type and message. Not all types of exceptions accept a
86 * custom message.
87 *
88 * {@hide}
89 */
90 @NonNull
Remi NGUYEN VANe55a88d2022-04-20 15:59:16 +090091 public static QosCallbackException createException(@ExceptionType final int type) {
Daniel Brightf9e945b2020-06-15 16:10:01 -070092 switch (type) {
93 case EX_TYPE_FILTER_NETWORK_RELEASED:
94 return new QosCallbackException(new NetworkReleasedException());
95 case EX_TYPE_FILTER_SOCKET_NOT_BOUND:
96 return new QosCallbackException(new SocketNotBoundException());
sewookseoafc22b02022-03-07 11:03:13 +000097 case EX_TYPE_FILTER_SOCKET_NOT_CONNECTED:
98 return new QosCallbackException(new SocketNotConnectedException());
Daniel Brightf9e945b2020-06-15 16:10:01 -070099 case EX_TYPE_FILTER_NOT_SUPPORTED:
100 return new QosCallbackException(new UnsupportedOperationException(
101 "This device does not support the specified filter"));
102 case EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED:
103 return new QosCallbackException(
104 new SocketLocalAddressChangedException());
sewookseoafc22b02022-03-07 11:03:13 +0000105 case EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED:
106 return new QosCallbackException(
107 new SocketRemoteAddressChangedException());
Daniel Brightf9e945b2020-06-15 16:10:01 -0700108 default:
109 Log.wtf(TAG, "create: No case setup for exception type: '" + type + "'");
110 return new QosCallbackException(
111 new RuntimeException("Unknown exception code: " + type));
112 }
113 }
114
sewookseoe7c48142022-03-10 03:06:43 +0000115 @VisibleForTesting
Daniel Brightf9e945b2020-06-15 16:10:01 -0700116 public QosCallbackException(@NonNull final String message) {
117 super(message);
118 }
119
sewookseoe7c48142022-03-10 03:06:43 +0000120 @VisibleForTesting
Daniel Brightf9e945b2020-06-15 16:10:01 -0700121 public QosCallbackException(@NonNull final Throwable cause) {
122 super(cause);
123 }
124}