Skip to content

Commit

Permalink
Use NoSuchFileException for missing files (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n authored Dec 2, 2023
1 parent 0721081 commit f9549ce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/main/java/cpw/mods/cl/ModuleClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import cpw.mods.util.LambdaExceptionUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
Expand All @@ -13,6 +12,7 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.NoSuchFileException;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Consumer;
Expand Down Expand Up @@ -279,8 +279,11 @@ protected Class<?> findClass(final String moduleName, final String name) {
}

protected <T> T loadFromModule(final String moduleName, BiFunction<ModuleReader, ModuleReference, T> lookup) throws IOException {
var module = configuration.findModule(moduleName).orElseThrow(FileNotFoundException::new);
var ref = module.reference();
var module = configuration.findModule(moduleName);
if (module.isEmpty()) {
throw new NoSuchFileException("module " + moduleName);
}
var ref = module.get().reference();
try (var reader = ref.open()) {
return lookup.apply(reader, ref);
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/cpw/mods/niofs/union/UnionFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,12 @@ private Path toRealPath(final Path basePath, final UnionPath path) {

public SeekableByteChannel newReadByteChannel(final UnionPath path) throws IOException {
try {
return findFirstFiltered(path)
.map(this::byteChannel)
.orElseThrow(FileNotFoundException::new);
var ret = findFirstFiltered(path).map(this::byteChannel);
if (ret.isPresent()) {
return ret.get();
} else {
throw new NoSuchFileException(path.toString());
}
} catch (UncheckedIOException ioe) {
throw ioe.getCause();
}
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/cpw/mods/niofs/union/TestUnionFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand Down Expand Up @@ -267,7 +266,7 @@ public void testDirectoryVisitorJar() throws Exception {
.peek(path -> foundFiles.add(path.toString()))
.map(p-> ()-> {
if (!Files.exists(p)) {
throw new FileNotFoundException(p.toString());
throw new NoSuchFileException(p.toString());
}
})
);
Expand Down

0 comments on commit f9549ce

Please sign in to comment.