Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,17 @@ public static Metadata matchSingleFileSpec(String spec) throws IOException {
if (matchResult.status() == Status.NOT_FOUND) {
throw new FileNotFoundException(String.format("File spec %s not found", spec));
} else if (matchResult.status() != Status.OK) {
// PROPOSED FIX: Extract the root cause by catching it from metadata()
IOException rootCause = null;
try {
matchResult.metadata();
} catch (IOException e) {
rootCause = e;
}
// Chain the root cause into the new IOException
throw new IOException(
String.format("Error matching file spec %s: status %s", spec, matchResult.status()));
String.format("Error matching file spec %s: status %s", spec, matchResult.status()),
rootCause);
} else {
List<Metadata> metadata = matchResult.metadata();
if (metadata.size() != 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ public class FileSystemsTest {
@Rule public ExpectedException thrown = ExpectedException.none();
private LocalFileSystem localFileSystem = new LocalFileSystem();

@Test
public void testMatchSingleFileSpecExceptionChaining() throws Exception {
java.io.IOException rootCause = new java.io.IOException("403 Forbidden: Fake GCS Error");
MatchResult failedResult = MatchResult.create(MatchResult.Status.ERROR, rootCause);

FileSystem mockFileSystem = mock(FileSystem.class);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to test this without mocking - e.g. by setting up a local filesystem to result in this error, or using an in-memory fake for a blobstore?

when(mockFileSystem.match(org.mockito.ArgumentMatchers.anyList()))
.thenReturn(ImmutableList.of(failedResult));

// Use reflection to temporarily override the registered filesystems
java.lang.reflect.Field field = FileSystems.class.getDeclaredField("SCHEME_TO_FILESYSTEM");
field.setAccessible(true);
@SuppressWarnings("unchecked")
java.util.concurrent.atomic.AtomicReference<java.util.Map<String, FileSystem>> ref =
(java.util.concurrent.atomic.AtomicReference<java.util.Map<String, FileSystem>>)
field.get(null);

java.util.Map<String, FileSystem> original = ref.get();
try {
ref.set(com.google.common.collect.ImmutableMap.of("dummy", mockFileSystem));

thrown.expect(java.io.IOException.class);
thrown.expectMessage("Error matching file spec dummy://fake/path: status ERROR");
thrown.expectCause(org.hamcrest.Matchers.is(rootCause));

FileSystems.matchSingleFileSpec("dummy://fake/path");
} finally {
// Restore the original registry so we don't break other tests
ref.set(original);
}
}

@Test
public void testGetLocalFileSystem() throws Exception {
// TODO: Java core test failing on windows, https://github.com/apache/beam/issues/20484
Expand Down
Loading