1/*
2 * Copyright (C) 2015 Square, Inc.
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 */
16package com.squareup.okhttp.internal.http;
17
18import java.io.IOException;
19import java.lang.reflect.InvocationTargetException;
20import java.lang.reflect.Method;
21
22/**
23 * An exception thrown to indicate a problem connecting via a single Route. Multiple attempts may
24 * have been made with alternative protocols, none of which were successful.
25 */
26public final class RouteException extends Exception {
27  private static final Method addSuppressedExceptionMethod;
28  static {
29    Method m;
30    try {
31      m = Throwable.class.getDeclaredMethod("addSuppressed", Throwable.class);
32    } catch (Exception e) {
33      m = null;
34    }
35    addSuppressedExceptionMethod = m;
36  }
37  private IOException lastException;
38
39  public RouteException(IOException cause) {
40    super(cause);
41    lastException = cause;
42  }
43
44  public IOException getLastConnectException() {
45    return lastException;
46  }
47
48  public void addConnectException(IOException e) {
49    addSuppressedIfPossible(e, lastException);
50    lastException = e;
51  }
52
53  private void addSuppressedIfPossible(IOException e, IOException suppressed) {
54    if (addSuppressedExceptionMethod != null) {
55      try {
56        addSuppressedExceptionMethod.invoke(e, suppressed);
57      } catch (InvocationTargetException | IllegalAccessException ignored) {
58      }
59    }
60  }
61}
62