1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.internal; 17 18 import com.google.android.libraries.mobiledatadownload.DownloadException; 19 import java.io.IOException; 20 import java.util.concurrent.CancellationException; 21 import java.util.concurrent.ExecutionException; 22 23 /** 24 * Maps exception to MddLibApiResult.Code. Used for logging. 25 * 26 * @see wireless.android.icing.proto.MddLibApiResult 27 */ 28 public final class ExceptionToMddResultMapper { 29 ExceptionToMddResultMapper()30 private ExceptionToMddResultMapper() {} 31 32 /** 33 * Maps Exception to appropriate int for logging. 34 * 35 * <p>If t is an ExecutionException, then the cause (t.getCause()) is mapped. 36 */ map(Throwable t)37 public static int map(Throwable t) { 38 39 Throwable cause; 40 if (t instanceof ExecutionException) { 41 cause = t.getCause(); 42 } else { 43 cause = t; 44 } 45 46 if (cause instanceof CancellationException) { 47 return 0; 48 } else if (cause instanceof InterruptedException) { 49 return 0; 50 } else if (cause instanceof IOException) { 51 return 0; 52 } else if (cause instanceof IllegalStateException) { 53 return 0; 54 } else if (cause instanceof IllegalArgumentException) { 55 return 0; 56 } else if (cause instanceof UnsupportedOperationException) { 57 return 0; 58 } else if (cause instanceof DownloadException) { 59 return 0; 60 } 61 62 // Capturing all other errors occurred during execution as unknown errors. 63 return 0; 64 } 65 } 66