Skip to content

Commit 060047e

Browse files
jean-philippe-martinlbergelson
authored andcommitted
include cause when SAMFileWriterFactory rethrows exceptions (#1023)
* RunTimeIOExceptions thrown in SAMFileWriterFactory now propagate the original IOException that caused them.
1 parent 0ab0667 commit 060047e

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/main/java/htsjdk/samtools/SAMFileWriterFactory.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import htsjdk.samtools.util.zip.DeflaterFactory;
3333

3434
import java.io.File;
35-
import java.io.FileOutputStream;
3635
import java.io.IOException;
3736
import java.io.OutputStream;
3837
import java.nio.file.Files;
@@ -300,7 +299,7 @@ public SAMFileWriter makeBAMWriter(final SAMFileHeader header, final boolean pre
300299
if (this.useAsyncIo) return new AsyncSAMFileWriter(ret, this.asyncOutputBufferSize);
301300
else return ret;
302301
} catch (final IOException ioe) {
303-
throw new RuntimeIOException("Error opening file: " + outputPath.toUri());
302+
throw new RuntimeIOException("Error opening file: " + outputPath.toUri(), ioe);
304303
}
305304
}
306305

@@ -352,7 +351,7 @@ public SAMFileWriter makeSAMWriter(final SAMFileHeader header, final boolean pre
352351
samFlagFieldOutput);
353352
return initWriter(header, presorted, ret);
354353
} catch (final IOException ioe) {
355-
throw new RuntimeIOException("Error opening file: " + outputPath.toUri());
354+
throw new RuntimeIOException("Error opening file: " + outputPath.toUri(), ioe);
356355
}
357356
}
358357

@@ -588,7 +587,7 @@ private CRAMFileWriter createCRAMWriterWithSettings(
588587
indexOS = Files.newOutputStream(indexPath);
589588
}
590589
catch (final IOException ioe) {
591-
throw new RuntimeIOException("Error creating index file for: " + indexPath.toUri());
590+
throw new RuntimeIOException("Error creating index file for: " + indexPath.toUri(), ioe);
592591
}
593592
}
594593
}
@@ -597,7 +596,7 @@ private CRAMFileWriter createCRAMWriterWithSettings(
597596
cramOS = IOUtil.maybeBufferOutputStream(Files.newOutputStream(outputFile), bufferSize);
598597
}
599598
catch (final IOException ioe) {
600-
throw new RuntimeIOException("Error creating CRAM file: " + outputFile.toUri());
599+
throw new RuntimeIOException("Error creating CRAM file: " + outputFile.toUri(), ioe);
601600
}
602601

603602
final Path md5Path = IOUtil.addExtension(outputFile, ".md5");

src/test/java/htsjdk/samtools/SAMFileWriterFactoryTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
import htsjdk.samtools.cram.build.CramIO;
3030
import htsjdk.samtools.cram.ref.ReferenceSource;
3131
import htsjdk.samtools.util.IOUtil;
32+
import htsjdk.samtools.util.RuntimeIOException;
3233
import java.nio.file.Path;
3334
import java.nio.file.Files;
3435
import java.nio.file.FileSystem;
36+
import java.nio.file.Paths;
3537
import org.testng.Assert;
3638
import org.testng.annotations.DataProvider;
3739
import org.testng.annotations.Test;
@@ -76,6 +78,17 @@ public void ordinaryPathWriterTest() throws Exception {
7678
}
7779
}
7880

81+
@Test()
82+
public void pathWriterFailureMentionsCause() throws Exception {
83+
try {
84+
final Path outputPath = Paths.get("nope://no.txt");
85+
createSmallBam(outputPath);
86+
Assert.fail("Should have thrown a RuntimeIOException");
87+
} catch (RuntimeIOException expected) {
88+
Assert.assertTrue(expected.getCause().toString().contains("NoSuchFileException"));
89+
}
90+
}
91+
7992
@Test(description="create a BAM in memory, should start with GZipInputStream.GZIP_MAGIC")
8093
public void inMemoryBam() throws Exception {
8194
ByteArrayOutputStream os=new ByteArrayOutputStream();

0 commit comments

Comments
 (0)