blob: 1a397b39ef3c159a1026a2e1e1516dda912e0c99 [file] [log] [blame]
Jeff Sharkeya1031142014-07-12 18:09:46 -07001/*
2 * Copyright (C) 2014 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.util;
18
Eugene Susla47aafbe2017-02-13 12:46:46 -080019import android.annotation.NonNull;
Eugene Suslaa38fbf62017-03-14 10:26:10 -070020import android.annotation.Nullable;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070021import android.os.ParcelableException;
22
Eugene Susla47aafbe2017-02-13 12:46:46 -080023import com.android.internal.util.Preconditions;
24
Jeff Sharkeya1031142014-07-12 18:09:46 -070025import java.io.IOException;
26
27/**
28 * Utility methods for proxying richer exceptions across Binder calls.
29 *
30 * @hide
31 */
32public class ExceptionUtils {
Jeff Sharkeya1031142014-07-12 18:09:46 -070033 public static RuntimeException wrap(IOException e) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070034 throw new ParcelableException(e);
Jeff Sharkeya1031142014-07-12 18:09:46 -070035 }
36
37 public static void maybeUnwrapIOException(RuntimeException e) throws IOException {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070038 if (e instanceof ParcelableException) {
39 ((ParcelableException) e).maybeRethrow(IOException.class);
Jeff Sharkeya1031142014-07-12 18:09:46 -070040 }
41 }
Jeff Sharkey78a13012014-07-15 20:18:34 -070042
43 public static String getCompleteMessage(String msg, Throwable t) {
44 final StringBuilder builder = new StringBuilder();
45 if (msg != null) {
46 builder.append(msg).append(": ");
47 }
48 builder.append(t.getMessage());
49 while ((t = t.getCause()) != null) {
50 builder.append(": ").append(t.getMessage());
51 }
52 return builder.toString();
53 }
54
55 public static String getCompleteMessage(Throwable t) {
56 return getCompleteMessage(null, t);
57 }
Eugene Susla47aafbe2017-02-13 12:46:46 -080058
Eugene Suslaa38fbf62017-03-14 10:26:10 -070059 public static <E extends Throwable> void propagateIfInstanceOf(
60 @Nullable Throwable t, Class<E> c) throws E {
61 if (t != null && c.isInstance(t)) {
62 throw c.cast(t);
63 }
64 }
65
66 /**
67 * @param <E> a checked exception that is ok to throw without wrapping
68 */
69 public static <E extends Exception> RuntimeException propagate(@NonNull Throwable t, Class<E> c)
70 throws E {
71 propagateIfInstanceOf(t, c);
72 return propagate(t);
73 }
74
Eugene Susla47aafbe2017-02-13 12:46:46 -080075 public static RuntimeException propagate(@NonNull Throwable t) {
76 Preconditions.checkNotNull(t);
Eugene Suslaa38fbf62017-03-14 10:26:10 -070077 propagateIfInstanceOf(t, Error.class);
78 propagateIfInstanceOf(t, RuntimeException.class);
Eugene Susla47aafbe2017-02-13 12:46:46 -080079 throw new RuntimeException(t);
80 }
Eugene Susla4f8680b2017-08-07 17:25:30 -070081
82 /**
83 * Gets the root {@link Throwable#getCause() cause} of {@code t}
84 */
85 public static @NonNull Throwable getRootCause(@NonNull Throwable t) {
86 while (t.getCause() != null) t = t.getCause();
87 return t;
88 }
Eugene Susla5210e942018-02-14 12:59:29 -080089
90 /**
91 * Appends {@code cause} at the end of the causal chain of {@code t}
92 *
93 * @return {@code t} for convenience
94 */
95 public static @NonNull Throwable appendCause(@NonNull Throwable t, @Nullable Throwable cause) {
96 if (cause != null) {
97 getRootCause(t).initCause(cause);
98 }
99 return t;
100 }
101}