org.conqat.lib.commons.error.ExceptionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-lib-commons Show documentation
Show all versions of teamscale-lib-commons Show documentation
Provides common utility functions
/*
* Copyright (c) CQSE GmbH
*
* 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 org.conqat.lib.commons.error;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.conqat.lib.commons.assertion.CCSMAssert;
import org.conqat.lib.commons.function.RunnableWithException;
import org.conqat.lib.commons.string.StringUtils;
/**
* Utility functionality for exception handling.
*/
public class ExceptionUtils {
/**
* Trims the given {@link StackOverflowError}'s stacktrace by omitting all redundant stack trace
* elements.
*/
public static void trimStackOverFlowTrace(StackOverflowError e) {
List trimmedStackTrace = new ArrayList<>();
StackTraceElement previousElement = null;
StackTraceElement[] stackTrace = e.getStackTrace();
for (int i = 0; i < stackTrace.length; i++) {
StackTraceElement currentElement = stackTrace[i];
if (!currentElement.equals(previousElement)) {
trimmedStackTrace.add(stackTrace[i]);
previousElement = currentElement;
}
}
e.setStackTrace(trimmedStackTrace.toArray(new StackTraceElement[trimmedStackTrace.size()]));
}
/**
* Obtains the stack trace of the given {@link Throwable} as a string.
*/
public static String getStacktraceAsString(Throwable t) {
return StringUtils.obtainStackTrace(t);
}
/**
* Adds the {@code suppressed} as {@link Throwable#addSuppressed(Throwable) suppressed} to the
* {@code suppressing} Throwable.
*
* @return The {@code suppressing} Throwable.
*/
public static T withSuppressed(@NonNull T suppressing, @NonNull Throwable suppressed) {
suppressing.addSuppressed(suppressed);
return suppressing;
}
/**
* Returns the first non-null message in the cause hierarchy of the given {@link Throwable}. Will
* return {@link Optional#empty()} if none is found.
*/
public static Optional getFirstNonNullCauseMessage(Throwable e) {
if (e.getMessage() != null) {
return Optional.of(e.getMessage());
}
Throwable currentStack = e;
while (currentStack.getCause() != null) {
currentStack = currentStack.getCause();
if (currentStack.getMessage() != null) {
return Optional.of(currentStack.getMessage());
}
}
return Optional.empty();
}
/**
* Runs all the {@code runnables} even in case of exceptions. Any thrown exceptions are collected
* and added as suppressed exceptions to the first thrown exception, which is then re-thrown.
*/
@SafeVarargs
public static void runAllAndAddAsSuppressed(Class exceptionClass,
RunnableWithException... runnables) throws E {
ExceptionHolder holder = null;
for (RunnableWithException runnable : runnables) {
try {
runnable.run();
} catch (Exception e) {
// Should either be of type E or RuntimeException
if (holder != null) {
holder.addSuppressed(e);
} else {
holder = new ExceptionHolder<>(exceptionClass, e);
}
}
}
if (holder != null) {
holder.rethrow();
}
}
private static final class ExceptionHolder {
private final Exception exception;
private ExceptionHolder(Class exceptionType, Exception exception) {
CCSMAssert.isNotNull(exceptionType, () -> String.format("Expected \"%s\" to be not null", "exceptionType"));
CCSMAssert.isNotNull(exception, () -> String.format("Expected \"%s\" to be not null", "exception"));
if (!(exception instanceof RuntimeException || exceptionType.isInstance(exception))) {
throw new AssertionError("The Expected exception was not of type RuntimeException or " + exceptionType,
exception);
}
this.exception = exception;
}
/**
* Appends the specified {@code suppressed} to the exceptions that were suppressed in order to
* deliver the underlying exception.
*
* @see Throwable#addSuppressed(Throwable)
*/
private void addSuppressed(Throwable suppressed) {
if (suppressed != exception) {
exception.addSuppressed(suppressed);
}
}
/**
* @throws E
* The original exception
*/
@SuppressWarnings("unchecked") // Type was verified during construction
private void rethrow() throws E {
// During runtime the cast will have no effect due to type-erasure,
// so this will also work if "exception" is actually a RuntimeException and not
// of type E
throw (E) exception;
}
}
}