/
ExceptionUtil.java
197 lines (175 loc) · 7.74 KB
/
ExceptionUtil.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.internal.util;
import com.hazelcast.core.HazelcastException;
import com.hazelcast.instance.impl.OutOfMemoryErrorDispatcher;
import com.hazelcast.logging.ILogger;
import javax.annotation.Nonnull;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
/**
* Contains various exception related utility methods.
*/
public final class ExceptionUtil {
private static final BiFunction<Throwable, String, HazelcastException> HAZELCAST_EXCEPTION_WRAPPER = (throwable, message) -> {
if (message != null) {
return new HazelcastException(message, throwable);
} else {
return new HazelcastException(throwable);
}
};
private ExceptionUtil() {
}
/**
* Converts a Throwable stacktrace to a String.
*
* @param cause the Throwable
* @return the String.
*/
public static String toString(Throwable cause) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
cause.printStackTrace(pw);
return sw.toString();
}
public static RuntimeException peel(final Throwable t) {
return (RuntimeException) peel(t, null, null, HAZELCAST_EXCEPTION_WRAPPER);
}
/**
* Processes {@code Throwable t} so that the returned {@code Throwable}'s type matches {@code allowedType} or
* {@code RuntimeException}. Processing may include unwrapping {@code t}'s cause hierarchy, wrapping it in a
* {@code HazelcastException} or just returning the same instance {@code t} if it is already an instance of
* {@code RuntimeException}.
*
* @param t {@code Throwable} to be peeled
* @param allowedType the type expected to be returned; when {@code null}, this method returns instances
* of {@code RuntimeException}
* @param message if not {@code null}, used as the message in the {@code HazelcastException} that
* may wrap the peeled {@code Throwable}
* @param <T> expected type of {@code Throwable}
* @return the peeled {@code Throwable}
*/
public static <T extends Throwable> Throwable peel(final Throwable t, Class<T> allowedType, String message) {
return peel(t, allowedType, message, HAZELCAST_EXCEPTION_WRAPPER);
}
/**
* Processes {@code Throwable t} so that the returned {@code Throwable}'s type matches {@code allowedType},
* {@code RuntimeException} or any {@code Throwable} returned by `exceptionWrapper`
* Processing may include unwrapping {@code t}'s cause hierarchy, wrapping it in a exception
* created by using exceptionWrapper or just returning the same instance {@code t}
* if it is already an instance of {@code RuntimeException}.
*
* @param t {@code Throwable} to be peeled
* @param allowedType the type expected to be returned; when {@code null}, this method returns instances
* of {@code RuntimeException} or <W>
* @param message if not {@code null}, used as the message in {@code RuntimeException} that
* may wrap the peeled {@code Throwable}
* @param exceptionWrapper wraps the peeled code using this exceptionWrapper
* @param <W> Type of the wrapper exception in exceptionWrapper
* @param <T> allowed type of {@code Throwable}
* @return the peeled {@code Throwable}
*/
public static <T, W extends Throwable> Throwable peel(final Throwable t, Class<T> allowedType,
String message, BiFunction<Throwable, String, W> exceptionWrapper) {
if (t instanceof RuntimeException) {
return t;
}
if (t instanceof ExecutionException || t instanceof InvocationTargetException) {
final Throwable cause = t.getCause();
if (cause != null) {
return peel(cause, allowedType, message, exceptionWrapper);
} else {
return exceptionWrapper.apply(t, message);
}
}
if (allowedType != null && allowedType.isAssignableFrom(t.getClass())) {
return t;
}
return exceptionWrapper.apply(t, message);
}
public static RuntimeException rethrow(Throwable t) {
rethrowIfError(t);
throw peel(t);
}
public static RuntimeException rethrow(Throwable t, BiFunction<Throwable, String, RuntimeException> exceptionWrapper) {
rethrowIfError(t);
throw (RuntimeException) peel(t, null, null, exceptionWrapper);
}
public static <T extends Throwable> RuntimeException rethrow(final Throwable t, Class<T> allowedType) throws T {
rethrowIfError(t);
throw (T) peel(t, allowedType, null);
}
/**
* This rethrow the exception providing an allowed Exception in first priority, even it is a Runtime exception
*/
public static <T extends Throwable> RuntimeException rethrowAllowedTypeFirst(final Throwable t,
Class<T> allowedType) throws T {
rethrowIfError(t);
if (allowedType.isAssignableFrom(t.getClass())) {
throw (T) t;
} else {
throw peel(t);
}
}
public static void rethrowIfError(final Throwable t) {
if (t instanceof Error) {
if (t instanceof OutOfMemoryError) {
OutOfMemoryErrorDispatcher.onOutOfMemory((OutOfMemoryError) t);
}
throw (Error) t;
}
}
public static RuntimeException rethrowAllowInterrupted(final Throwable t) throws InterruptedException {
return rethrow(t, InterruptedException.class);
}
@SuppressWarnings("unchecked")
@Nonnull
public static <T extends Throwable> RuntimeException sneakyThrow(@Nonnull Throwable t) throws T {
throw (T) t;
}
/**
* Utility to make sure exceptions inside
* {@link java.util.concurrent.CompletionStage#whenComplete(BiConsumer)} are not swallowed.
* Exceptions will be caught and logged using the supplied logger.
*/
@Nonnull
public static <T> BiConsumer<T, ? super Throwable> withTryCatch(
@Nonnull ILogger logger, @Nonnull BiConsumer<T, ? super Throwable> consumer
) {
return withTryCatch(logger, "Exception during callback", consumer);
}
/**
* Utility to make sure exceptions inside
* {@link java.util.concurrent.CompletionStage#whenComplete(BiConsumer)} are not swallowed.
* Exceptions will be caught and logged using the supplied logger and message.
*/
@Nonnull
public static <T> BiConsumer<T, ? super Throwable> withTryCatch(
@Nonnull ILogger logger, @Nonnull String message, @Nonnull BiConsumer<T, ? super Throwable> consumer
) {
return (r, t) -> {
try {
consumer.accept(r, t);
} catch (Throwable e) {
logger.severe(message, e);
}
};
}
}