blob: b80cff415e536b42262ec4dff10f97331cf0a27e [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 //
60 /** {@hide} */
61 public static final int EX_TYPE_FILTER_NONE = 0;
62
63 /** {@hide} */
64 public static final int EX_TYPE_FILTER_NETWORK_RELEASED = 1;
65
66 /** {@hide} */
67 public static final int EX_TYPE_FILTER_SOCKET_NOT_BOUND = 2;
68
69 /** {@hide} */
sewookseoafc22b02022-03-07 11:03:13 +000070 public static final int EX_TYPE_FILTER_SOCKET_NOT_CONNECTED = 3;
Daniel Brightf9e945b2020-06-15 16:10:01 -070071
72 /** {@hide} */
sewookseoafc22b02022-03-07 11:03:13 +000073 public static final int EX_TYPE_FILTER_NOT_SUPPORTED = 4;
74
75 /** {@hide} */
76 public static final int EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED = 5;
77
78 /** {@hide} */
79 public static final int EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED = 6;
Daniel Brightf9e945b2020-06-15 16:10:01 -070080
81 /**
82 * Creates exception based off of a type and message. Not all types of exceptions accept a
83 * custom message.
84 *
85 * {@hide}
86 */
87 @NonNull
88 static QosCallbackException createException(@ExceptionType final int type) {
89 switch (type) {
90 case EX_TYPE_FILTER_NETWORK_RELEASED:
91 return new QosCallbackException(new NetworkReleasedException());
92 case EX_TYPE_FILTER_SOCKET_NOT_BOUND:
93 return new QosCallbackException(new SocketNotBoundException());
sewookseoafc22b02022-03-07 11:03:13 +000094 case EX_TYPE_FILTER_SOCKET_NOT_CONNECTED:
95 return new QosCallbackException(new SocketNotConnectedException());
Daniel Brightf9e945b2020-06-15 16:10:01 -070096 case EX_TYPE_FILTER_NOT_SUPPORTED:
97 return new QosCallbackException(new UnsupportedOperationException(
98 "This device does not support the specified filter"));
99 case EX_TYPE_FILTER_SOCKET_LOCAL_ADDRESS_CHANGED:
100 return new QosCallbackException(
101 new SocketLocalAddressChangedException());
sewookseoafc22b02022-03-07 11:03:13 +0000102 case EX_TYPE_FILTER_SOCKET_REMOTE_ADDRESS_CHANGED:
103 return new QosCallbackException(
104 new SocketRemoteAddressChangedException());
Daniel Brightf9e945b2020-06-15 16:10:01 -0700105 default:
106 Log.wtf(TAG, "create: No case setup for exception type: '" + type + "'");
107 return new QosCallbackException(
108 new RuntimeException("Unknown exception code: " + type));
109 }
110 }
111
sewookseoe7c48142022-03-10 03:06:43 +0000112 @VisibleForTesting
Daniel Brightf9e945b2020-06-15 16:10:01 -0700113 public QosCallbackException(@NonNull final String message) {
114 super(message);
115 }
116
sewookseoe7c48142022-03-10 03:06:43 +0000117 @VisibleForTesting
Daniel Brightf9e945b2020-06-15 16:10:01 -0700118 public QosCallbackException(@NonNull final Throwable cause) {
119 super(cause);
120 }
121}