1 /* 2 * Copyright (C) 2023 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 android.net.http; 18 19 import org.chromium.net.CronetException; 20 21 import java.io.IOException; 22 23 public class CronetExceptionTranslationUtils { CronetExceptionTranslationUtils()24 private CronetExceptionTranslationUtils() {} 25 26 public interface CronetRunnable<T> { run()27 T run() throws IOException; 28 } 29 executeTranslatingExceptions( CronetRunnable<T> work)30 public static <T> T executeTranslatingExceptions( 31 CronetRunnable<T> work) throws IOException { 32 try { 33 return work.run(); 34 } catch (CronetException e) { 35 throw translateException(e); 36 } 37 } 38 translateException(CronetException e)39 public static HttpException translateException(CronetException e) { 40 if (e instanceof org.chromium.net.QuicException) { 41 return new QuicExceptionWrapper((org.chromium.net.QuicException) e); 42 } 43 44 if (e instanceof org.chromium.net.NetworkException) { 45 return new NetworkExceptionWrapper((org.chromium.net.NetworkException) e); 46 } 47 48 if (e instanceof org.chromium.net.CallbackException) { 49 return new CallbackExceptionWrapper((org.chromium.net.CallbackException) e); 50 } 51 52 return new CronetExceptionWrapper(e); 53 } 54 maybeTranslateException(Throwable t)55 public static Throwable maybeTranslateException(Throwable t) { 56 if (t instanceof org.chromium.net.InlineExecutionProhibitedException) { 57 // InlineExecutionProhibitedException is final, so we can't wrap it. 58 android.net.http.InlineExecutionProhibitedException translatedException = 59 new android.net.http.InlineExecutionProhibitedException(); 60 translatedException.initCause(t); 61 return translatedException; 62 } 63 64 return t; 65 } 66 } 67