From 3b5659834a066c27a991c742649d976b535dc18a Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Tue, 13 Aug 2024 11:17:58 +0200 Subject: [PATCH] Improve exception readability in case a directory can't be deleted because 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 ``` --- .../org/jvnet/hudson/test/TemporaryDirectoryAllocator.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jvnet/hudson/test/TemporaryDirectoryAllocator.java b/src/main/java/org/jvnet/hudson/test/TemporaryDirectoryAllocator.java index 5d3b50219..818a992aa 100644 --- a/src/main/java/org/jvnet/hudson/test/TemporaryDirectoryAllocator.java +++ b/src/main/java/org/jvnet/hudson/test/TemporaryDirectoryAllocator.java @@ -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. @@ -140,9 +141,11 @@ private void delete(Path p) throws IOException { } Files.deleteIfExists(p); } catch (DirectoryNotEmptyException x) { + String pathString = p.toString(); try (Stream 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; } }