LUCENE-6714: Expose exception details via getters on CorruptedIndex-, IndexTooOld-, IndexTooOldException

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1693929 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2015-08-03 16:22:10 +00:00
parent 0398139b1a
commit bf853057ce
5 changed files with 163 additions and 22 deletions

View File

@ -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());
}
// pass
if (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());
}
// pass
if (VERBOSE) {
System.out.println("TEST: got expected exc:");

View File

@ -98,6 +98,7 @@ public class BuildMaxPositionIndex {
try {
w.forceMerge(1);
} catch (CorruptIndexException cie) {
assertEquals(cie.getMessage(), new CorruptIndexException(cie.getOriginalMessage(), cie.getResourceDescription()).getMessage());
// SerialMergeScheduler
assertTrue("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)"));

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}