OpenSearch/src/main/java/org/elasticsearch/ExceptionsHelper.java

188 lines
6.0 KiB
Java
Raw Normal View History

2010-02-08 15:30:06 +02:00
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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
2010-02-08 15:30:06 +02:00
*
* 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.elasticsearch;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.rest.RestStatus;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
2010-02-08 15:30:06 +02:00
/**
2011-12-06 02:42:25 +02:00
*
2010-02-08 15:30:06 +02:00
*/
public final class ExceptionsHelper {
private static final ESLogger logger = Loggers.getLogger(ExceptionsHelper.class);
public static RuntimeException convertToRuntime(Throwable t) {
if (t instanceof RuntimeException) {
return (RuntimeException) t;
}
return new ElasticsearchException(t.getMessage(), t);
}
public static ElasticsearchException convertToElastic(Throwable t) {
if (t instanceof ElasticsearchException) {
return (ElasticsearchException) t;
}
return new ElasticsearchException(t.getMessage(), t);
}
public static RestStatus status(Throwable t) {
if (t instanceof ElasticsearchException) {
return ((ElasticsearchException) t).status();
}
return RestStatus.INTERNAL_SERVER_ERROR;
}
2010-02-08 15:30:06 +02:00
public static Throwable unwrapCause(Throwable t) {
int counter = 0;
2010-02-08 15:30:06 +02:00
Throwable result = t;
while (result instanceof ElasticsearchWrapperException) {
2010-07-01 10:27:53 +03:00
if (result.getCause() == null) {
return result;
}
2010-07-01 10:27:53 +03:00
if (result.getCause() == result) {
return result;
}
if (counter++ > 10) {
// dear god, if we got more than 10 levels down, WTF? just bail
logger.warn("Exception cause unwrapping ran for 10 levels...", t);
return result;
}
2010-07-01 10:28:57 +03:00
result = result.getCause();
2010-02-08 15:30:06 +02:00
}
return result;
}
public static String detailedMessage(Throwable t) {
return detailedMessage(t, false, 0);
}
2010-02-08 15:30:06 +02:00
public static String detailedMessage(Throwable t, boolean newLines, int initialCounter) {
if (t == null) {
return "Unknown";
}
2010-02-08 15:30:06 +02:00
int counter = initialCounter + 1;
if (t.getCause() != null) {
StringBuilder sb = new StringBuilder();
while (t != null) {
sb.append(t.getClass().getSimpleName());
2010-02-08 15:30:06 +02:00
if (t.getMessage() != null) {
sb.append("[");
2010-02-08 15:30:06 +02:00
sb.append(t.getMessage());
2010-02-22 08:55:36 +02:00
sb.append("]");
}
if (!newLines) {
sb.append("; ");
2010-02-08 15:30:06 +02:00
}
t = t.getCause();
if (t != null) {
if (newLines) {
sb.append("\n");
for (int i = 0; i < counter; i++) {
sb.append("\t");
}
} else {
sb.append("nested: ");
}
}
counter++;
}
return sb.toString();
} else {
2010-02-22 08:55:36 +02:00
return t.getClass().getSimpleName() + "[" + t.getMessage() + "]";
2010-02-08 15:30:06 +02:00
}
}
public static String stackTrace(Throwable e) {
StringWriter stackTraceStringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stackTraceStringWriter);
e.printStackTrace(printWriter);
return stackTraceStringWriter.toString();
}
/**
* Rethrows the first exception in the list and adds all remaining to the suppressed list.
* If the given list is empty no exception is thrown
*
*/
public static <T extends Throwable> void rethrowAndSuppress(List<T> exceptions) throws T {
T main = null;
for (T ex : exceptions) {
main = useOrSupress(main, ex);
}
if (main != null) {
throw main;
}
}
/**
* Throws a runtime exception with all given exceptions added as suppressed.
* If the given list is empty no exception is thrown
*/
public static <T extends Throwable> void maybeThrowRuntimeAndSuppress(List<T> exceptions) {
T main = null;
for (T ex : exceptions) {
main = useOrSupress(main, ex);
}
if (main != null) {
throw new ElasticsearchException(main.getMessage(), main);
}
}
public static <T extends Throwable> T useOrSupress(T first, T second) {
if (first == null) {
return second;
} else {
first.addSuppressed(second);
}
return first;
}
public static <T extends Throwable> T unwrap(Throwable t, Class<T> clazz) {
if (t != null) {
do {
if (clazz.isInstance(t)) {
return clazz.cast(t);
}
} while ((t = t.getCause()) != null);
}
return null;
}
/**
* Returns <code>true</code> iff the given throwable is and OutOfMemoryException, otherwise <code>false</code>
*/
public static boolean isOOM(Throwable t) {
return t != null
&& (t instanceof OutOfMemoryError
|| (t instanceof IllegalStateException
&& t.getMessage() != null
&& t.getMessage().contains("OutOfMemoryError")
)
);
}
2010-02-08 15:30:06 +02:00
}