Skip to content

Commit

Permalink
A better diagnostic error for corrupt or missing JARs.
Browse files Browse the repository at this point in the history
Augment the IOException with the name of the file we're trying
to open.

Motivated by a troubleshooting session with partial downloads
of JARs from Maven central breaking the Scala build on Martin's
laptop.

The test case only tests our part of the error message, so as not
to be platform / JDK specific. Otherwise, it would check that the
correct cause exception was present and accounted for.
  • Loading branch information
retronym committed Aug 27, 2013
1 parent 4254b47 commit 5e8bc19
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/reflect/scala/reflect/io/ZipArchive.scala
Expand Up @@ -126,7 +126,11 @@ abstract class ZipArchive(override val file: JFile) extends AbstractFile with Eq
/** ''Note: This library is considered experimental and should not be used unless you know what you are doing.'' */
final class FileZipArchive(file: JFile) extends ZipArchive(file) {
def iterator: Iterator[Entry] = {
val zipFile = new ZipFile(file)
val zipFile = try {
new ZipFile(file)
} catch {
case ioe: IOException => throw new IOException("Error accessing " + file.getPath, ioe)
}
val root = new DirEntry("/")
val dirs = mutable.HashMap[String, DirEntry]("/" -> root)
val enum = zipFile.entries()
Expand Down
37 changes: 37 additions & 0 deletions test/junit/scala/reflect/io/ZipArchiveTest.scala
@@ -0,0 +1,37 @@
package scala.reflect.io

import java.io.{IOException, File => JFile}
import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(classOf[JUnit4])
class ZipArchiveTest {

@Test
def corruptZip {
val f = JFile.createTempFile("test", ".jar")
val fza = new FileZipArchive(f)
try {
fza.iterator
} catch {
case x: IOException =>
assertTrue(x.getMessage, x.getMessage.contains(f.getPath))
} finally {
f.delete()
}
}

@Test
def missingFile {
val f = new JFile("xxx.does.not.exist")
val fza = new FileZipArchive(f)
try {
fza.iterator
} catch {
case x: IOException =>
assertTrue(x.getMessage, x.getMessage.contains(f.getPath))
}
}
}

0 comments on commit 5e8bc19

Please sign in to comment.