Apache Lucene open-source search software
LUCENE-6714: Expose exception details via getters on CorruptedIndex-, IndexTooOld-, IndexTooOldException
Simon Willnauerbf853057cea1
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.javaindex dae837c1428..5662ba3d63b 100644--- a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java+++ b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java@@ -502,6 +502,19 @@ public class TestBackwardsCompatibility extends LuceneTestCase {reader = DirectoryReader.open(dir);fail("DirectoryReader.open should not pass for "+unsupportedNames[i]);} catch (IndexFormatTooOldException e) {+ if (e.getReason() != null) {+ assertNull(e.getVersion());+ assertNull(e.getMinVersion());+ assertNull(e.getMaxVersion());+ assertEquals(e.getMessage(), new IndexFormatTooOldException(e.getResourceDescription(), e.getReason()).getMessage());+ } else {+ assertNotNull(e.getVersion());+ assertNotNull(e.getMinVersion());+ assertNotNull(e.getMaxVersion());+ assertTrue(e.getMessage(), e.getMaxVersion() >= e.getMinVersion());+ assertTrue(e.getMessage(), e.getMaxVersion() < e.getVersion() || e.getVersion() < e.getMinVersion());+ assertEquals(e.getMessage(), new IndexFormatTooOldException(e.getResourceDescription(), e.getVersion(), e.getMinVersion(), e.getMaxVersion()).getMessage());+ }// passif (VERBOSE) {System.out.println("TEST: got expected exc:");@@ -516,6 +529,19 @@ public class TestBackwardsCompatibility extends LuceneTestCase {writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())).setCommitOnClose(false));fail("IndexWriter creation should not pass for "+unsupportedNames[i]);} catch (IndexFormatTooOldException e) {+ if (e.getReason() != null) {+ assertNull(e.getVersion());+ assertNull(e.getMinVersion());+ assertNull(e.getMaxVersion());+ assertEquals(e.getMessage(), new IndexFormatTooOldException(e.getResourceDescription(), e.getReason()).getMessage());+ } else {+ assertNotNull(e.getVersion());+ assertNotNull(e.getMinVersion());+ assertNotNull(e.getMaxVersion());+ assertTrue(e.getMessage(), e.getMaxVersion() >= e.getMinVersion());+ assertTrue(e.getMessage(), e.getMaxVersion() < e.getVersion() || e.getVersion() < e.getMinVersion());+ assertEquals(e.getMessage(), new IndexFormatTooOldException(e.getResourceDescription(), e.getVersion(), e.getMinVersion(), e.getMaxVersion()).getMessage());+ }// passif (VERBOSE) {System.out.println("TEST: got expected exc:");diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestMaxPositionInOldIndex.java b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestMaxPositionInOldIndex.javaindex 28ac18bf607..f5aafbd7f3b 100644--- a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestMaxPositionInOldIndex.java+++ b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestMaxPositionInOldIndex.java@@ -98,6 +98,7 @@ public class BuildMaxPositionIndex {try {w.forceMerge(1);} catch (CorruptIndexException cie) {+ assertEquals(cie.getMessage(), new CorruptIndexException(cie.getOriginalMessage(), cie.getResourceDescription()).getMessage());// SerialMergeSchedulerassertTrue("got message " + cie.getMessage(),cie.getMessage().contains("position=2147483647 is too large (> IndexWriter.MAX_POSITION=2147483519), field=\"foo\" doc=0 (resource=PerFieldPostings(segment=_0 formats=1)"));diff --git a/lucene/core/src/java/org/apache/lucene/index/CorruptIndexException.java b/lucene/core/src/java/org/apache/lucene/index/CorruptIndexException.javaindex 5553a089a9c..3fff88fdcba 100644--- a/lucene/core/src/java/org/apache/lucene/index/CorruptIndexException.java+++ b/lucene/core/src/java/org/apache/lucene/index/CorruptIndexException.java@@ -28,6 +28,10 @@ import org.apache.lucene.store.DataOutput;* an inconsistency in the index.*/public class CorruptIndexException extends IOException {++ private final String message;+ private final String resourceDescription;+/** Create exception with a message only */public CorruptIndexException(String message, DataInput input) {this(message, input, null);@@ -56,5 +60,21 @@ public class CorruptIndexException extends IOException {/** Create exception with message and root cause. */public CorruptIndexException(String message, String resourceDescription, Throwable cause) {super(Objects.toString(message) + " (resource=" + resourceDescription + ")", cause);+ this.resourceDescription = resourceDescription;+ this.message = message;+ }++ /**+ * Returns a description of the file that was corrupted+ */+ public String getResourceDescription() {+ return resourceDescription;+ }++ /**+ * Returns the original exception message without the corrupted file description.+ */+ public String getOriginalMessage() {+ return message;}}diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexFormatTooNewException.java b/lucene/core/src/java/org/apache/lucene/index/IndexFormatTooNewException.javaindex b23fb4d20e8..8d4fe1d58a3 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexFormatTooNewException.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexFormatTooNewException.java@@ -28,30 +28,65 @@ import org.apache.lucene.store.DataInput;*/public class IndexFormatTooNewException extends IOException {+ private final String resourceDescription;+ private final int version;+ private final int minVersion;+ private final int maxVersion;+/** Creates an {@code IndexFormatTooNewException}*- * @param resourceDesc describes the file that was too old- * @param version the version of the file that was too old+ * @param resourceDescription describes the file that was too new+ * @param version the version of the file that was too new* @param minVersion the minimum version accepted- * @param maxVersion the maxium version accepted+ * @param maxVersion the maximum version accepted** @lucene.internal */- public IndexFormatTooNewException(String resourceDesc, int version, int minVersion, int maxVersion) {- super("Format version is not supported (resource " + resourceDesc + "): "+ public IndexFormatTooNewException(String resourceDescription, int version, int minVersion, int maxVersion) {+ super("Format version is not supported (resource " + resourceDescription + "): "+ version + " (needs to be between " + minVersion + " and " + maxVersion + ")");+ this.resourceDescription = resourceDescription;+ this.version = version;+ this.minVersion = minVersion;+ this.maxVersion = maxVersion;}/** Creates an {@code IndexFormatTooNewException}*- * @param in the open file that's too old- * @param version the version of the file that was too old+ * @param in the open file that's too new+ * @param version the version of the file that was too new* @param minVersion the minimum version accepted- * @param maxVersion the maxium version accepted+ * @param maxVersion the maximum version accepted** @lucene.internal */public IndexFormatTooNewException(DataInput in, int version, int minVersion, int maxVersion) {this(Objects.toString(in), version, minVersion, maxVersion);}+ /**+ * Returns a description of the file that was too new+ */+ public String getResourceDescription() {+ return resourceDescription;+ }++ /**+ * Returns the version of the file that was too new+ */+ public int getVersion() {+ return version;+ }+ /**+ * Returns the maximum version accepted+ */+ public int getMaxVersion() {+ return maxVersion;+ }++ /**+ * Returns the minimum version accepted+ */+ public int getMinVersion() {+ return minVersion;+ }}diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexFormatTooOldException.java b/lucene/core/src/java/org/apache/lucene/index/IndexFormatTooOldException.javaindex 150b7f107b0..72f2276d163 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexFormatTooOldException.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexFormatTooOldException.java@@ -28,39 +28,57 @@ import org.apache.lucene.store.DataInput;*/public class IndexFormatTooOldException extends IOException {+ private final String resourceDescription;+ private final String reason;+ private final Integer version;+ private final Integer minVersion;+ private final Integer maxVersion;++/** Creates an {@code IndexFormatTooOldException}.*- * @param resourceDesc describes the file that was too old- * @param version the version of the file that was too old- *+ * @param resourceDescription describes the file that was too old+ * @param reason the reason for this exception if the version is not available+ ** @lucene.internal */- public IndexFormatTooOldException(String resourceDesc, String version) {- super("Format version is not supported (resource " + resourceDesc + "): " +- version + ". This version of Lucene only supports indexes created with release 4.0 and later.");+ public IndexFormatTooOldException(String resourceDescription, String reason) {+ super("Format version is not supported (resource " + resourceDescription + "): " ++ reason + ". This version of Lucene only supports indexes created with release 4.0 and later.");+ this.resourceDescription = resourceDescription;+ this.reason = reason;+ this.version = null;+ this.minVersion = null;+ this.maxVersion = null;+}/** Creates an {@code IndexFormatTooOldException}.** @param in the open file that's too old- * @param version the version of the file that was too old+ * @param reason the reason for this exception if the version is not available** @lucene.internal */- public IndexFormatTooOldException(DataInput in, String version) {- this(Objects.toString(in), version);+ public IndexFormatTooOldException(DataInput in, String reason) {+ this(Objects.toString(in), reason);}/** Creates an {@code IndexFormatTooOldException}.*- * @param resourceDesc describes the file that was too old+ * @param resourceDescription describes the file that was too old* @param version the version of the file that was too old* @param minVersion the minimum version accepted- * @param maxVersion the maxium version accepted+ * @param maxVersion the maximum version accepted** @lucene.internal */- public IndexFormatTooOldException(String resourceDesc, int version, int minVersion, int maxVersion) {- super("Format version is not supported (resource " + resourceDesc + "): " ++ public IndexFormatTooOldException(String resourceDescription, int version, int minVersion, int maxVersion) {+ super("Format version is not supported (resource " + resourceDescription + "): " +version + " (needs to be between " + minVersion + " and " + maxVersion +"). This version of Lucene only supports indexes created with release 4.0 and later.");+ this.resourceDescription = resourceDescription;+ this.version = version;+ this.minVersion = minVersion;+ this.maxVersion = maxVersion;+ this.reason = null;}/** Creates an {@code IndexFormatTooOldException}.@@ -68,10 +86,51 @@ public class IndexFormatTooOldException extends IOException {* @param in the open file that's too old* @param version the version of the file that was too old* @param minVersion the minimum version accepted- * @param maxVersion the maxium version accepted+ * @param maxVersion the maximum version accepted** @lucene.internal */public IndexFormatTooOldException(DataInput in, int version, int minVersion, int maxVersion) {this(Objects.toString(in), version, minVersion, maxVersion);}++ /**+ * Returns a description of the file that was too old+ */+ public String getResourceDescription() {+ return resourceDescription;+ }++ /**+ * Returns an optional reason for this exception if the version information was not available. Otherwise <code>null</code>+ */+ public String getReason() {+ return reason;+ }++ /**+ * Returns the version of the file that was too old.+ * This method will return <code>null</code> if an alternative {@link #getReason()}+ * is provided.+ */+ public Integer getVersion() {+ return version;+ }++ /**+ * Returns the maximum version accepted.+ * This method will return <code>null</code> if an alternative {@link #getReason()}+ * is provided.+ */+ public Integer getMaxVersion() {+ return maxVersion;+ }++ /**+ * Returns the minimum version accepted+ * This method will return <code>null</code> if an alternative {@link #getReason()}+ * is provided.+ */+ public Integer getMinVersion() {+ return minVersion;+ }}