Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.util; |
| 18 | |
Eugene Susla | 47aafbe | 2017-02-13 12:46:46 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 21 | import android.os.ParcelableException; |
| 22 | |
Eugene Susla | 47aafbe | 2017-02-13 12:46:46 -0800 | [diff] [blame] | 23 | import com.android.internal.util.Preconditions; |
| 24 | |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 25 | import java.io.IOException; |
| 26 | |
| 27 | /** |
| 28 | * Utility methods for proxying richer exceptions across Binder calls. |
| 29 | * |
| 30 | * @hide |
| 31 | */ |
| 32 | public class ExceptionUtils { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 33 | public static RuntimeException wrap(IOException e) { |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 34 | throw new ParcelableException(e); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | public static void maybeUnwrapIOException(RuntimeException e) throws IOException { |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 38 | if (e instanceof ParcelableException) { |
| 39 | ((ParcelableException) e).maybeRethrow(IOException.class); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
Jeff Sharkey | 78a1301 | 2014-07-15 20:18:34 -0700 | [diff] [blame] | 42 | |
| 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 Susla | 47aafbe | 2017-02-13 12:46:46 -0800 | [diff] [blame] | 58 | |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 59 | 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 Susla | 47aafbe | 2017-02-13 12:46:46 -0800 | [diff] [blame] | 75 | public static RuntimeException propagate(@NonNull Throwable t) { |
| 76 | Preconditions.checkNotNull(t); |
Eugene Susla | a38fbf6 | 2017-03-14 10:26:10 -0700 | [diff] [blame] | 77 | propagateIfInstanceOf(t, Error.class); |
| 78 | propagateIfInstanceOf(t, RuntimeException.class); |
Eugene Susla | 47aafbe | 2017-02-13 12:46:46 -0800 | [diff] [blame] | 79 | throw new RuntimeException(t); |
| 80 | } |
Eugene Susla | 4f8680b | 2017-08-07 17:25:30 -0700 | [diff] [blame] | 81 | |
| 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 Susla | 5210e94 | 2018-02-14 12:59:29 -0800 | [diff] [blame] | 89 | |
| 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 | } |