add more essential exceptions

This commit is contained in:
Simon Willnauer 2015-06-30 13:25:50 +02:00
parent 971ac0475e
commit 6ee9a3d5f2
4 changed files with 73 additions and 24 deletions

View File

@ -557,7 +557,7 @@ public class ElasticsearchException extends RuntimeException implements ToXConte
} }
mapping.put(name, constructor); mapping.put(name, constructor);
} catch (NoSuchMethodException t) { } 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);
} }
} }

View File

@ -22,6 +22,8 @@ package org.elasticsearch.common.io.stream;
import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexFormatTooNewException; import org.apache.lucene.index.IndexFormatTooNewException;
import org.apache.lucene.index.IndexFormatTooOldException; 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.BytesRef;
import org.apache.lucene.util.CharsRefBuilder; import org.apache.lucene.util.CharsRefBuilder;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
@ -36,6 +38,7 @@ import org.joda.time.DateTime;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import java.io.*; import java.io.*;
import java.nio.file.NoSuchFileException;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -499,39 +502,41 @@ public abstract class StreamInput extends InputStream {
msg = msg.substring(0, idx); msg = msg.substring(0, idx);
return (T) readStackTrace(new CorruptIndexException(msg, resource, readThrowable()), this); // Lucene 5.3 will have getters for all these return (T) readStackTrace(new CorruptIndexException(msg, resource, readThrowable()), this); // Lucene 5.3 will have getters for all these
case 2: case 2:
final String itnMessage = readOptionalString(); return (T) readStackTrace(new IndexFormatTooNewException(readOptionalString(), -1, -1, -1), this); // Lucene 5.3 will have getters for all these
readThrowable();
return (T) readStackTrace(new IndexFormatTooNewException(itnMessage, -1, -1, -1), this);
case 3: case 3:
final String itoMessage = readOptionalString(); return (T) readStackTrace(new IndexFormatTooOldException(readOptionalString(), -1, -1, -1), this); // Lucene 5.3 will have getters for all these
readThrowable();
return (T) readStackTrace(new IndexFormatTooOldException(itoMessage, -1, -1, -1), this);
case 4: case 4:
final String npeMessage = readOptionalString(); return (T) readStackTrace(new NullPointerException(readOptionalString()), this);
readThrowable();
return (T) readStackTrace(new NullPointerException(npeMessage), this);
case 5: case 5:
final String nfeMessage = readOptionalString(); return (T) readStackTrace(new NumberFormatException(readOptionalString()), this);
readThrowable();
return (T) readStackTrace(new NumberFormatException(nfeMessage), this);
case 6: case 6:
return (T) readStackTrace(new IllegalArgumentException(readOptionalString(), readThrowable()), this); return (T) readStackTrace(new IllegalArgumentException(readOptionalString(), readThrowable()), this);
case 7: case 7:
return (T) readStackTrace(new IllegalStateException(readOptionalString(), readThrowable()), this); return (T) readStackTrace(new IllegalStateException(readOptionalString(), readThrowable()), this);
case 8: case 8:
final String eofMessage = readOptionalString(); return (T) readStackTrace(new EOFException(readOptionalString()), this);
readThrowable();
return (T) readStackTrace(new EOFException(eofMessage), this);
case 9: case 9:
return (T) readStackTrace(new SecurityException(readOptionalString(), readThrowable()), this); return (T) readStackTrace(new SecurityException(readOptionalString(), readThrowable()), this);
case 10: case 10:
final String sidxMessage = readOptionalString(); return (T) readStackTrace(new StringIndexOutOfBoundsException(readOptionalString()), this);
readThrowable();
return (T) readStackTrace(new StringIndexOutOfBoundsException(sidxMessage), this);
case 11: case 11:
final String aidxMessage = readOptionalString(); return (T) readStackTrace(new ArrayIndexOutOfBoundsException(readOptionalString()), this);
readThrowable(); case 12:
return (T) readStackTrace(new ArrayIndexOutOfBoundsException(aidxMessage), this); 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: default:
assert false : "no such exception for id: " + key; assert false : "no such exception for id: " + key;
} }

View File

@ -19,9 +19,12 @@
package org.elasticsearch.common.io.stream; package org.elasticsearch.common.io.stream;
import com.vividsolutions.jts.util.Assert;
import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexFormatTooNewException; import org.apache.lucene.index.IndexFormatTooNewException;
import org.apache.lucene.index.IndexFormatTooOldException; 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.BytesRef;
import org.apache.lucene.util.BytesRefBuilder; import org.apache.lucene.util.BytesRefBuilder;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
@ -32,8 +35,10 @@ import org.elasticsearch.common.text.Text;
import org.joda.time.ReadableInstant; import org.joda.time.ReadableInstant;
import java.io.EOFException; import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.NoSuchFileException;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -453,28 +458,54 @@ public abstract class StreamOutput extends OutputStream {
writeBoolean(false); writeBoolean(false);
} else { } else {
writeBoolean(true); writeBoolean(true);
boolean writeCause = true;
if (throwable instanceof CorruptIndexException) { if (throwable instanceof CorruptIndexException) {
writeVInt(1); writeVInt(1);
} else if (throwable instanceof IndexFormatTooNewException) { } else if (throwable instanceof IndexFormatTooNewException) {
writeVInt(2); writeVInt(2);
writeCause = false;
} else if (throwable instanceof IndexFormatTooOldException) { } else if (throwable instanceof IndexFormatTooOldException) {
writeVInt(3); writeVInt(3);
writeCause = false;
} else if (throwable instanceof NullPointerException) { } else if (throwable instanceof NullPointerException) {
writeVInt(4); writeVInt(4);
writeCause = false;
} else if (throwable instanceof NumberFormatException) { } else if (throwable instanceof NumberFormatException) {
writeVInt(5); writeVInt(5);
writeCause = false;
} else if (throwable instanceof IllegalArgumentException) { } else if (throwable instanceof IllegalArgumentException) {
writeVInt(6); writeVInt(6);
} else if (throwable instanceof IllegalStateException) { } else if (throwable instanceof IllegalStateException) {
writeVInt(7); writeVInt(7);
} else if (throwable instanceof EOFException) { } else if (throwable instanceof EOFException) {
writeVInt(8); writeVInt(8);
writeCause = false;
} else if (throwable instanceof SecurityException) { } else if (throwable instanceof SecurityException) {
writeVInt(9); writeVInt(9);
} else if (throwable instanceof StringIndexOutOfBoundsException) { } else if (throwable instanceof StringIndexOutOfBoundsException) {
writeVInt(10); writeVInt(10);
writeCause = false;
} else if (throwable instanceof ArrayIndexOutOfBoundsException) { } else if (throwable instanceof ArrayIndexOutOfBoundsException) {
writeVInt(11); 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 { } else {
ElasticsearchException ex; ElasticsearchException ex;
final String name = throwable.getClass().getName(); final String name = throwable.getClass().getName();
@ -490,7 +521,9 @@ public abstract class StreamOutput extends OutputStream {
} }
writeOptionalString(throwable.getMessage()); writeOptionalString(throwable.getMessage());
writeThrowable(throwable.getCause()); if (writeCause) {
writeThrowable(throwable.getCause());
}
ElasticsearchException.writeStackTraces(throwable, this); ElasticsearchException.writeStackTraces(throwable, this);
} }
} }

View File

@ -22,6 +22,8 @@ package org.elasticsearch;
import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexFormatTooNewException; import org.apache.lucene.index.IndexFormatTooNewException;
import org.apache.lucene.index.IndexFormatTooOldException; 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.SearchPhaseExecutionException;
import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
@ -43,6 +45,7 @@ import org.junit.Test;
import java.io.EOFException; import java.io.EOFException;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.NoSuchFileException;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
@ -264,6 +267,14 @@ public class ElasticsearchExceptionTests extends ElasticsearchTestCase {
new CorruptIndexException("baaaam", "this is my resource"), new CorruptIndexException("baaaam", "this is my resource"),
new IndexFormatTooNewException("tooo new", 1, 1, 1), new IndexFormatTooNewException("tooo new", 1, 1, 1),
new IndexFormatTooOldException("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 new Throwable("this exception is unknown", new QueryParsingException(new Index("foo"), 1, 2, "foobar", null) ), // somethin unknown
}; };
for (Throwable t : causes) { for (Throwable t : causes) {
@ -277,7 +288,7 @@ public class ElasticsearchExceptionTests extends ElasticsearchTestCase {
// these don't work yet - missing ctors // these don't work yet - missing ctors
assertNotEquals(e.getCause().getMessage(), ex.getCause().getMessage()); assertNotEquals(e.getCause().getMessage(), ex.getCause().getMessage());
} else { } 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()); assertEquals(e.getCause().getClass(), e.getCause().getClass());
assertArrayEquals(e.getStackTrace(), ex.getStackTrace()); assertArrayEquals(e.getStackTrace(), ex.getStackTrace());