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 * @hide 31 */ 32 public class ExceptionUtils { 33 /** TODO add javadoc */ wrap(IOException e)34 public static RuntimeException wrap(IOException e) { 35 throw new ParcelableException(e); 36 } 37 38 /** TODO add javadoc */ maybeUnwrapIOException(RuntimeException e)39 public static void maybeUnwrapIOException(RuntimeException e) throws IOException { 40 if (e instanceof ParcelableException) { 41 ((ParcelableException) e).maybeRethrow(IOException.class); 42 } 43 } 44 45 /** TODO add javadoc */ getCompleteMessage(String msg, Throwable throwable)46 public static String getCompleteMessage(String msg, Throwable throwable) { 47 Throwable t = throwable; 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 throwable)92 public static @NonNull Throwable getRootCause(@NonNull Throwable throwable) { 93 Throwable t = throwable; 94 while (t.getCause() != null) t = t.getCause(); 95 return t; 96 } 97 98 /** 99 * Appends {@code cause} at the end of the causal chain of {@code t} 100 * 101 * @return {@code t} for convenience 102 */ appendCause(@onNull Throwable t, @Nullable Throwable cause)103 public static @NonNull Throwable appendCause(@NonNull Throwable t, @Nullable Throwable cause) { 104 if (cause != null) { 105 getRootCause(t).initCause(cause); 106 } 107 return t; 108 } 109 110 111 } 112