Extensions to the ErrorThrowing framework for testing purposes

This commit is contained in:
Eric Banks 2012-09-06 22:03:18 -04:00
parent cb84a6473f
commit 576c7280d9
3 changed files with 51 additions and 19 deletions

View File

@ -112,31 +112,31 @@ public class CommandLineGATK extends CommandLineExecutable {
}
}
protected static final String PICARD_TEXT_SAM_FILE_ERROR_1 = "Cannot use index file with textual SAM file";
protected static final String PICARD_TEXT_SAM_FILE_ERROR_2 = "Cannot retrieve file pointers within SAM text files";
public static final String PICARD_TEXT_SAM_FILE_ERROR_1 = "Cannot use index file with textual SAM file";
public static final String PICARD_TEXT_SAM_FILE_ERROR_2 = "Cannot retrieve file pointers within SAM text files";
private static void checkForMaskedUserErrors(final Throwable t) {
final String message = t.getMessage();
if ( message == null )
return;
// we know what to do about the common "Too many open files" error
if ( message.indexOf("Too many open files") != -1 )
if ( message.contains("Too many open files") )
exitSystemWithUserError(new UserException.TooManyOpenFiles());
// malformed BAM looks like a SAM file
if ( message.indexOf(PICARD_TEXT_SAM_FILE_ERROR_1) != -1 ||
message.indexOf(PICARD_TEXT_SAM_FILE_ERROR_2) != -1 )
if ( message.contains(PICARD_TEXT_SAM_FILE_ERROR_1) ||
message.contains(PICARD_TEXT_SAM_FILE_ERROR_2) )
exitSystemWithSamError(t);
// can't close tribble index when writing
if ( message.indexOf("Unable to close index for") != -1 )
if ( message.contains("Unable to close index for") )
exitSystemWithUserError(new UserException(t.getCause() == null ? message : t.getCause().getMessage()));
// disk is full
if ( message.indexOf("No space left on device") != -1 )
exitSystemWithUserError(new UserException(t.getMessage()));
if ( t.getCause() != null && t.getCause().getMessage().indexOf("No space left on device") != -1 )
exitSystemWithUserError(new UserException(t.getCause().getMessage()));
if ( message.contains("No space left on device") )
exitSystemWithUserError(new UserException.NoSpaceOnDevice());
if ( t.getCause() != null && t.getCause().getMessage().contains("No space left on device") )
exitSystemWithUserError(new UserException.NoSpaceOnDevice());
}
/**

View File

@ -24,6 +24,7 @@
package org.broadinstitute.sting.gatk.walkers.qc;
import org.broadinstitute.sting.commandline.Argument;
import org.broadinstitute.sting.commandline.Hidden;
import org.broadinstitute.sting.commandline.Input;
import org.broadinstitute.sting.gatk.CommandLineGATK;
@ -45,20 +46,23 @@ public class ErrorThrowing extends RodWalker<Integer,Integer> implements TreeRed
@Input(fullName="exception", shortName = "E", doc="Java class of exception to throw", required=true)
public String exceptionToThrow;
@Argument(fullName = "failMethod", shortName = "fail", doc = "Determines which method to fail in", required = false)
public FailMethod failMethod = FailMethod.MAP;
public enum FailMethod {
MAP,
REDUCE,
TREE_REDUCE
}
//
// Template code to allow us to build the walker, doesn't actually do anything
//
@Override
public Integer map(RefMetaDataTracker tracker, ReferenceContext ref, AlignmentContext context) {
if ( exceptionToThrow.equals("UserException") ) {
throw new UserException("UserException");
} else if ( exceptionToThrow.equals("NullPointerException") ) {
throw new NullPointerException();
} else if ( exceptionToThrow.equals("ReviewedStingException") ) {
throw new ReviewedStingException("ReviewedStingException");
} else {
throw new UserException.BadArgumentValue("exception", "exception isn't a recognized value " + exceptionToThrow);
}
if ( failMethod == FailMethod.MAP )
fail();
return 0;
}
@Override
@ -68,10 +72,32 @@ public class ErrorThrowing extends RodWalker<Integer,Integer> implements TreeRed
@Override
public Integer reduce(Integer value, Integer sum) {
if ( failMethod == FailMethod.REDUCE )
fail();
return value + sum;
}
public Integer treeReduce(final Integer lhs, final Integer rhs) {
if ( failMethod == FailMethod.TREE_REDUCE )
fail();
return lhs + rhs;
}
private void fail() {
if ( exceptionToThrow.equals("UserException") ) {
throw new UserException("UserException");
} else if ( exceptionToThrow.equals("NullPointerException") ) {
throw new NullPointerException();
} else if ( exceptionToThrow.equals("ReviewedStingException") ) {
throw new ReviewedStingException("ReviewedStingException");
} else if ( exceptionToThrow.equals("SamError1") ) {
throw new RuntimeException(CommandLineGATK.PICARD_TEXT_SAM_FILE_ERROR_1);
} else if ( exceptionToThrow.equals("SamError2") ) {
throw new RuntimeException(CommandLineGATK.PICARD_TEXT_SAM_FILE_ERROR_2);
} else if ( exceptionToThrow.equals("NoSpace") ) {
throw new net.sf.samtools.util.RuntimeIOException(new java.io.IOException("No space left on device java.io.FileOutputStream.writeBytes(Native Method)"));
} else {
throw new UserException.BadArgumentValue("exception", "exception isn't a recognized value " + exceptionToThrow);
}
}
}

View File

@ -141,6 +141,12 @@ public class UserException extends ReviewedStingException {
}
}
public static class NoSpaceOnDevice extends UserException {
public NoSpaceOnDevice() {
super("There is no space left on the device, so writing failed");
}
}
public static class CouldNotReadInputFile extends UserException {
public CouldNotReadInputFile(String message, Exception e) {
super(String.format("Couldn't read file because %s caused by %s", message, getMessage(e)));