2010-02-08 15:30:06 +02:00
|
|
|
/*
|
2014-01-06 22:48:02 +01: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;
|
|
|
|
|
2014-11-03 12:36:13 +01:00
|
|
|
import org.elasticsearch.common.Nullable;
|
2010-06-15 16:51:38 +03:00
|
|
|
import org.elasticsearch.common.logging.ESLogger;
|
|
|
|
import org.elasticsearch.common.logging.Loggers;
|
2012-07-02 00:04:49 +02:00
|
|
|
import org.elasticsearch.rest.RestStatus;
|
2010-05-22 02:13:39 +03:00
|
|
|
|
2014-02-12 21:53:23 +02:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
2014-06-17 22:54:57 +02:00
|
|
|
import java.util.List;
|
2014-02-12 21:53:23 +02:00
|
|
|
|
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 {
|
|
|
|
|
2010-05-22 02:13:39 +03:00
|
|
|
private static final ESLogger logger = Loggers.getLogger(ExceptionsHelper.class);
|
|
|
|
|
2013-08-14 23:41:00 +02:00
|
|
|
public static RuntimeException convertToRuntime(Throwable t) {
|
|
|
|
if (t instanceof RuntimeException) {
|
|
|
|
return (RuntimeException) t;
|
|
|
|
}
|
2014-01-06 21:58:46 +01:00
|
|
|
return new ElasticsearchException(t.getMessage(), t);
|
2013-08-14 23:41:00 +02:00
|
|
|
}
|
|
|
|
|
2014-01-06 21:58:46 +01:00
|
|
|
public static ElasticsearchException convertToElastic(Throwable t) {
|
|
|
|
if (t instanceof ElasticsearchException) {
|
|
|
|
return (ElasticsearchException) t;
|
2013-08-14 23:41:00 +02:00
|
|
|
}
|
2014-01-06 21:58:46 +01:00
|
|
|
return new ElasticsearchException(t.getMessage(), t);
|
2013-08-14 23:41:00 +02:00
|
|
|
}
|
|
|
|
|
2012-07-02 00:04:49 +02:00
|
|
|
public static RestStatus status(Throwable t) {
|
2014-01-06 21:58:46 +01:00
|
|
|
if (t instanceof ElasticsearchException) {
|
|
|
|
return ((ElasticsearchException) t).status();
|
2012-07-02 00:04:49 +02:00
|
|
|
}
|
|
|
|
return RestStatus.INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-02-08 15:30:06 +02:00
|
|
|
public static Throwable unwrapCause(Throwable t) {
|
2010-05-22 02:13:39 +03:00
|
|
|
int counter = 0;
|
2010-02-08 15:30:06 +02:00
|
|
|
Throwable result = t;
|
2014-01-06 21:58:46 +01:00
|
|
|
while (result instanceof ElasticsearchWrapperException) {
|
2010-07-01 10:27:53 +03:00
|
|
|
if (result.getCause() == null) {
|
2010-05-22 02:13:39 +03:00
|
|
|
return result;
|
|
|
|
}
|
2010-07-01 10:27:53 +03:00
|
|
|
if (result.getCause() == result) {
|
2010-05-22 02:13:39 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-02-20 17:35:26 +02:00
|
|
|
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) {
|
2010-02-22 00:05:32 +02:00
|
|
|
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) {
|
2012-06-08 11:30:27 +02:00
|
|
|
sb.append(t.getClass().getSimpleName());
|
2010-02-08 15:30:06 +02:00
|
|
|
if (t.getMessage() != null) {
|
2012-06-08 11:30:27 +02:00
|
|
|
sb.append("[");
|
2010-02-08 15:30:06 +02:00
|
|
|
sb.append(t.getMessage());
|
2010-02-22 08:55:36 +02:00
|
|
|
sb.append("]");
|
2012-06-08 11:30:27 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2014-02-12 21:53:23 +02:00
|
|
|
|
|
|
|
public static String stackTrace(Throwable e) {
|
|
|
|
StringWriter stackTraceStringWriter = new StringWriter();
|
|
|
|
PrintWriter printWriter = new PrintWriter(stackTraceStringWriter);
|
|
|
|
e.printStackTrace(printWriter);
|
|
|
|
return stackTraceStringWriter.toString();
|
|
|
|
}
|
2014-06-17 22:54:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2014-07-14 16:52:51 +02:00
|
|
|
main = useOrSupress(main, ex);
|
2014-06-17 22:54:57 +02:00
|
|
|
}
|
|
|
|
if (main != null) {
|
|
|
|
throw main;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 16:52:51 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-17 22:54:57 +02:00
|
|
|
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")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-11-03 12:36:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws the specified exception. If null if specified then <code>true</code> is returned.
|
|
|
|
*/
|
|
|
|
public static boolean reThrowIfNotNull(@Nullable Throwable e) {
|
|
|
|
if (e != null) {
|
|
|
|
if (e instanceof RuntimeException) {
|
|
|
|
throw (RuntimeException) e;
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
}
|