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