-
Notifications
You must be signed in to change notification settings - Fork 813
/
JdkErrorCauseExtractor.java
46 lines (38 loc) · 1.47 KB
/
JdkErrorCauseExtractor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.api.instrumenter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
final class JdkErrorCauseExtractor implements ErrorCauseExtractor {
static final ErrorCauseExtractor INSTANCE = new JdkErrorCauseExtractor();
@Nullable
private static final Class<?> COMPLETION_EXCEPTION_CLASS = getCompletionExceptionClass();
@Override
public Throwable extractCause(Throwable error) {
if (error.getCause() != null
&& (error instanceof ExecutionException
|| isInstanceOfCompletionException(error)
|| error instanceof InvocationTargetException
|| error instanceof UndeclaredThrowableException)) {
return extractCause(error.getCause());
}
return error;
}
private static boolean isInstanceOfCompletionException(Throwable error) {
return COMPLETION_EXCEPTION_CLASS != null && COMPLETION_EXCEPTION_CLASS.isInstance(error);
}
@Nullable
private static Class<?> getCompletionExceptionClass() {
try {
return Class.forName("java.util.concurrent.CompletionException");
} catch (ClassNotFoundException e) {
// Android level 21 does not support java.util.concurrent.CompletionException
return null;
}
}
private JdkErrorCauseExtractor() {}
}