diff --git a/core/src/main/java/org/elasticsearch/ElasticsearchException.java b/core/src/main/java/org/elasticsearch/ElasticsearchException.java index 978ac1185a9..cc71126f60f 100644 --- a/core/src/main/java/org/elasticsearch/ElasticsearchException.java +++ b/core/src/main/java/org/elasticsearch/ElasticsearchException.java @@ -557,7 +557,7 @@ public class ElasticsearchException extends RuntimeException implements ToXConte } mapping.put(name, constructor); } catch (NoSuchMethodException t) { - throw new RuntimeException("failed to register [" + name + "] ", t); + throw new RuntimeException("failed to register [" + name + "] exception must have a public StreamInput ctor", t); } } diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index 1affc8bd0d9..214b40d1218 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -22,6 +22,8 @@ package org.elasticsearch.common.io.stream; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexFormatTooNewException; import org.apache.lucene.index.IndexFormatTooOldException; +import org.apache.lucene.store.AlreadyClosedException; +import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.CharsRefBuilder; import org.elasticsearch.ElasticsearchException; @@ -36,6 +38,7 @@ import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import java.io.*; +import java.nio.file.NoSuchFileException; import java.util.*; import java.util.regex.Pattern; @@ -499,39 +502,41 @@ public abstract class StreamInput extends InputStream { msg = msg.substring(0, idx); return (T) readStackTrace(new CorruptIndexException(msg, resource, readThrowable()), this); // Lucene 5.3 will have getters for all these case 2: - final String itnMessage = readOptionalString(); - readThrowable(); - return (T) readStackTrace(new IndexFormatTooNewException(itnMessage, -1, -1, -1), this); + return (T) readStackTrace(new IndexFormatTooNewException(readOptionalString(), -1, -1, -1), this); // Lucene 5.3 will have getters for all these case 3: - final String itoMessage = readOptionalString(); - readThrowable(); - return (T) readStackTrace(new IndexFormatTooOldException(itoMessage, -1, -1, -1), this); + return (T) readStackTrace(new IndexFormatTooOldException(readOptionalString(), -1, -1, -1), this); // Lucene 5.3 will have getters for all these case 4: - final String npeMessage = readOptionalString(); - readThrowable(); - return (T) readStackTrace(new NullPointerException(npeMessage), this); + return (T) readStackTrace(new NullPointerException(readOptionalString()), this); case 5: - final String nfeMessage = readOptionalString(); - readThrowable(); - return (T) readStackTrace(new NumberFormatException(nfeMessage), this); + return (T) readStackTrace(new NumberFormatException(readOptionalString()), this); case 6: return (T) readStackTrace(new IllegalArgumentException(readOptionalString(), readThrowable()), this); case 7: return (T) readStackTrace(new IllegalStateException(readOptionalString(), readThrowable()), this); case 8: - final String eofMessage = readOptionalString(); - readThrowable(); - return (T) readStackTrace(new EOFException(eofMessage), this); + return (T) readStackTrace(new EOFException(readOptionalString()), this); case 9: return (T) readStackTrace(new SecurityException(readOptionalString(), readThrowable()), this); case 10: - final String sidxMessage = readOptionalString(); - readThrowable(); - return (T) readStackTrace(new StringIndexOutOfBoundsException(sidxMessage), this); + return (T) readStackTrace(new StringIndexOutOfBoundsException(readOptionalString()), this); case 11: - final String aidxMessage = readOptionalString(); - readThrowable(); - return (T) readStackTrace(new ArrayIndexOutOfBoundsException(aidxMessage), this); + return (T) readStackTrace(new ArrayIndexOutOfBoundsException(readOptionalString()), this); + case 12: + return (T) readStackTrace(new AssertionError(readOptionalString(), readThrowable()), this); + case 13: + return (T) readStackTrace(new FileNotFoundException(readOptionalString()), this); + case 14: + final String file = readOptionalString(); + final String other = readOptionalString(); + final String reason = readOptionalString(); + readOptionalString(); // skip the msg - it's composed from file, other and reason + return (T) readStackTrace(new NoSuchFileException(file, other, reason), this); + case 15: + return (T) readStackTrace(new OutOfMemoryError(readOptionalString()), this); + case 16: + return (T) readStackTrace(new AlreadyClosedException(readOptionalString(), readThrowable()), this); + case 17: + return (T) readStackTrace(new LockObtainFailedException(readOptionalString(), readThrowable()), this); default: assert false : "no such exception for id: " + key; } diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 75e81f7d6ec..c22880c28ff 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -19,9 +19,12 @@ package org.elasticsearch.common.io.stream; +import com.vividsolutions.jts.util.Assert; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexFormatTooNewException; import org.apache.lucene.index.IndexFormatTooOldException; +import org.apache.lucene.store.AlreadyClosedException; +import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefBuilder; import org.elasticsearch.ElasticsearchException; @@ -32,8 +35,10 @@ import org.elasticsearch.common.text.Text; import org.joda.time.ReadableInstant; import java.io.EOFException; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.NoSuchFileException; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; @@ -453,28 +458,54 @@ public abstract class StreamOutput extends OutputStream { writeBoolean(false); } else { writeBoolean(true); + boolean writeCause = true; if (throwable instanceof CorruptIndexException) { writeVInt(1); } else if (throwable instanceof IndexFormatTooNewException) { writeVInt(2); + writeCause = false; } else if (throwable instanceof IndexFormatTooOldException) { writeVInt(3); + writeCause = false; } else if (throwable instanceof NullPointerException) { writeVInt(4); + writeCause = false; } else if (throwable instanceof NumberFormatException) { writeVInt(5); + writeCause = false; } else if (throwable instanceof IllegalArgumentException) { writeVInt(6); } else if (throwable instanceof IllegalStateException) { writeVInt(7); } else if (throwable instanceof EOFException) { writeVInt(8); + writeCause = false; } else if (throwable instanceof SecurityException) { writeVInt(9); } else if (throwable instanceof StringIndexOutOfBoundsException) { writeVInt(10); + writeCause = false; } else if (throwable instanceof ArrayIndexOutOfBoundsException) { writeVInt(11); + writeCause = false; + } else if (throwable instanceof AssertionError) { + writeVInt(12); + } else if (throwable instanceof FileNotFoundException) { + writeVInt(13); + writeCause = false; + } else if (throwable instanceof NoSuchFileException) { + writeVInt(14); + writeOptionalString(((NoSuchFileException) throwable).getFile()); + writeOptionalString(((NoSuchFileException) throwable).getOtherFile()); + writeOptionalString(((NoSuchFileException) throwable).getReason()); + writeCause = false; + } else if (throwable instanceof OutOfMemoryError) { + writeVInt(15); + writeCause = false; + } else if (throwable instanceof AlreadyClosedException) { + writeVInt(16); + } else if (throwable instanceof LockObtainFailedException) { + writeVInt(17); } else { ElasticsearchException ex; final String name = throwable.getClass().getName(); @@ -490,7 +521,9 @@ public abstract class StreamOutput extends OutputStream { } writeOptionalString(throwable.getMessage()); - writeThrowable(throwable.getCause()); + if (writeCause) { + writeThrowable(throwable.getCause()); + } ElasticsearchException.writeStackTraces(throwable, this); } } diff --git a/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java b/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java index 5a30cb05b04..cb3f60285e9 100644 --- a/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java +++ b/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java @@ -22,6 +22,8 @@ package org.elasticsearch; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexFormatTooNewException; import org.apache.lucene.index.IndexFormatTooOldException; +import org.apache.lucene.store.AlreadyClosedException; +import org.apache.lucene.store.LockObtainFailedException; import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.common.io.stream.BytesStreamOutput; @@ -43,6 +45,7 @@ import org.junit.Test; import java.io.EOFException; import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.NoSuchFileException; import static org.hamcrest.Matchers.equalTo; @@ -264,6 +267,14 @@ public class ElasticsearchExceptionTests extends ElasticsearchTestCase { new CorruptIndexException("baaaam", "this is my resource"), new IndexFormatTooNewException("tooo new", 1, 1, 1), new IndexFormatTooOldException("tooo new", 1, 1, 1), + new ArrayIndexOutOfBoundsException("booom"), + new StringIndexOutOfBoundsException("booom"), + new FileNotFoundException("booom"), + new NoSuchFileException("booom"), + new AssertionError("booom", new NullPointerException()), + new OutOfMemoryError("no memory left"), + new AlreadyClosedException("closed!!", new NullPointerException()), + new LockObtainFailedException("can't lock directory", new NullPointerException()), new Throwable("this exception is unknown", new QueryParsingException(new Index("foo"), 1, 2, "foobar", null) ), // somethin unknown }; for (Throwable t : causes) { @@ -277,7 +288,7 @@ public class ElasticsearchExceptionTests extends ElasticsearchTestCase { // these don't work yet - missing ctors assertNotEquals(e.getCause().getMessage(), ex.getCause().getMessage()); } else { - assertEquals(e.getCause().getMessage(), ex.getCause().getMessage()); + assertEquals(ex.getCause().getClass().getName(), e.getCause().getMessage(), ex.getCause().getMessage()); } assertEquals(e.getCause().getClass(), e.getCause().getClass()); assertArrayEquals(e.getStackTrace(), ex.getStackTrace());