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