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 com.android.car.internal.util; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 22 import com.android.internal.util.Preconditions; 23 24 import java.io.IOException; 25 26 // Copied from frameworks/base 27 28 /** 29 * Utility methods for proxying richer exceptions across Binder calls. 30 * 31 * @hide 32 */ 33 public class ExceptionUtils { 34 /** TODO add javadoc */ wrap(IOException e)35 public static RuntimeException wrap(IOException e) { 36 throw new ParcelableException(e); 37 } 38 39 /** TODO add javadoc */ maybeUnwrapIOException(RuntimeException e)40 public static void maybeUnwrapIOException(RuntimeException e) throws IOException { 41 if (e instanceof ParcelableException) { 42 ((ParcelableException) e).maybeRethrow(IOException.class); 43 } 44 } 45 46 /** TODO add javadoc */ getCompleteMessage(String msg, Throwable t)47 public static String getCompleteMessage(String msg, Throwable t) { 48 final StringBuilder builder = new StringBuilder(); 49 if (msg != null) { 50 builder.append(msg).append(": "); 51 } 52 builder.append(t.getMessage()); 53 while ((t = t.getCause()) != null) { 54 builder.append(": ").append(t.getMessage()); 55 } 56 return builder.toString(); 57 } 58 59 /** TODO add javadoc */ getCompleteMessage(Throwable t)60 public static String getCompleteMessage(Throwable t) { 61 return getCompleteMessage(null, t); 62 } 63 64 /** TODO add javadoc */ propagateIfInstanceOf( @ullable Throwable t, Class<E> c)65 public static <E extends Throwable> void propagateIfInstanceOf( 66 @Nullable Throwable t, Class<E> c) throws E { 67 if (t != null && c.isInstance(t)) { 68 throw c.cast(t); 69 } 70 } 71 72 /** 73 * @param <E> a checked exception that is ok to throw without wrapping 74 */ propagate(@onNull Throwable t, Class<E> c)75 public static <E extends Exception> RuntimeException propagate(@NonNull Throwable t, Class<E> c) 76 throws E { 77 propagateIfInstanceOf(t, c); 78 return propagate(t); 79 } 80 81 /** TODO add javadoc */ propagate(@onNull Throwable t)82 public static RuntimeException propagate(@NonNull Throwable t) { 83 Preconditions.checkNotNull(t); 84 propagateIfInstanceOf(t, Error.class); 85 propagateIfInstanceOf(t, RuntimeException.class); 86 throw new RuntimeException(t); 87 } 88 89 /** 90 * Gets the root {@link Throwable#getCause() cause} of {@code t} 91 */ getRootCause(@onNull Throwable t)92 public static @NonNull Throwable getRootCause(@NonNull Throwable t) { 93 while (t.getCause() != null) t = t.getCause(); 94 return t; 95 } 96 97 /** 98 * Appends {@code cause} at the end of the causal chain of {@code t} 99 * 100 * @return {@code t} for convenience 101 */ appendCause(@onNull Throwable t, @Nullable Throwable cause)102 public static @NonNull Throwable appendCause(@NonNull Throwable t, @Nullable Throwable cause) { 103 if (cause != null) { 104 getRootCause(t).initCause(cause); 105 } 106 return t; 107 } 108 109 110 } 111