Skip to content

Commit

Permalink
Improve exception readability in case a directory can't be deleted be…
Browse files Browse the repository at this point in the history
…cause it still contains files

Use suppressed exceptions rather than wrap the exception so that we get
the primary cause first, then details.

Also resolve files relative to their containing folders.

Before:

```
java.io.IOException: [REDACTED]/target/tmp/j h16565971146678333138/users/Fred_10744520479289247763/config.xml
	at org.jvnet.hudson.test.TemporaryDirectoryAllocator.delete(TemporaryDirectoryAllocator.java:144)
	at org.jvnet.hudson.test.TemporaryDirectoryAllocator.delete(TemporaryDirectoryAllocator.java:131)
	at org.jvnet.hudson.test.TemporaryDirectoryAllocator.delete(TemporaryDirectoryAllocator.java:131)
	at org.jvnet.hudson.test.TemporaryDirectoryAllocator.dispose(TemporaryDirectoryAllocator.java:99)
	at org.jvnet.hudson.test.TestEnvironment.dispose(TestEnvironment.java:84)
	at org.jvnet.hudson.test.JenkinsRule.after(JenkinsRule.java:527)
	at org.jvnet.hudson.test.JenkinsRule$1.evaluate(JenkinsRule.java:665)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.nio.file.DirectoryNotEmptyException: [REDACTED]/target/tmp/j h16565971146678333138/users/Fred_10744520479289247763
	at java.base/sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:246)
	at java.base/sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(AbstractFileSystemProvider.java:110)
	at java.base/java.nio.file.Files.deleteIfExists(Files.java:1191)
	at org.jvnet.hudson.test.TemporaryDirectoryAllocator.delete(TemporaryDirectoryAllocator.java:141)
	... 8 more
```

After:

```
java.nio.file.DirectoryNotEmptyException: [REDACTED]/target/tmp/j h16565971146678333138/users/Fred_10744520479289247763
        at java.base/sun.nio.fs.UnixFileSystemProvider.implDelete(UnixFileSystemProvider.java:289)
        at java.base/sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(AbstractFileSystemProvider.java:109)
        at java.base/java.nio.file.Files.deleteIfExists(Files.java:1191)
        at org.jvnet.hudson.test.TemporaryDirectoryAllocator.delete(TemporaryDirectoryAllocator.java:142)
        at org.jvnet.hudson.test.TemporaryDirectoryAllocator.dispose(TemporaryDirectoryAllocator.java:100)
        at org.jvnet.hudson.test.TestEnvironment.dispose(TestEnvironment.java:84)
        at org.jvnet.hudson.test.JenkinsRule.after(JenkinsRule.java:586)
        at org.jvnet.hudson.test.JenkinsRule$1.evaluate(JenkinsRule.java:724)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
        at java.base/java.lang.Thread.run(Thread.java:1583)
        Suppressed: java.io.IOException: These files still exist : config.xml
                at org.jvnet.hudson.test.TemporaryDirectoryAllocator.delete(TemporaryDirectoryAllocator.java:146)
                ... 6 more
```
  • Loading branch information
Vlatombe committed Aug 13, 2024
1 parent 3585fb3 commit 3b56598
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang.StringUtils;

/**
* Allocates temporary directories and cleans it up at the end.
Expand Down Expand Up @@ -140,9 +141,11 @@ private void delete(Path p) throws IOException {
}
Files.deleteIfExists(p);
} catch (DirectoryNotEmptyException x) {
String pathString = p.toString();
try (Stream<Path> children = Files.list(p)) {
throw new IOException(children.map(Path::toString).collect(Collectors.joining(" ")), x);
x.addSuppressed(new IOException("These files still exist : " + children.map(Path::toString).map(s -> StringUtils.removeStart(s, pathString + File.separator)).collect(Collectors.joining(", "))));
}
throw x;
}
}

Expand Down

0 comments on commit 3b56598

Please sign in to comment.