Skip to content

Commit

Permalink
Throw only IOException from storage services
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Aug 6, 2024
1 parent 0636da9 commit 14ee5e4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.quarkus.arc.properties.IfBuildProperty;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.NotFoundException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -25,7 +24,7 @@ public byte[] fetch(final String path) throws IOException {
try (final var reader = new FileInputStream(getFile(path))) {
return reader.readAllBytes();
} catch (final FileNotFoundException e) {
throw new NotFoundException();
throw new IOException();
}
}

Expand All @@ -42,15 +41,15 @@ public void store(final String path, final byte[] data) throws IOException {

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void remove(final String path) {
public void remove(final String path) throws IOException {
getFile(path).delete();
}

private File getFile(final String path) {
private File getFile(final String path) throws IOException {
final var file = config.path().resolve(path).toFile();

if (!file.toPath().normalize().startsWith(config.path().normalize())) {
throw new NotFoundException();
throw new IOException();
}

return file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.quarkus.arc.Unremovable;
import io.quarkus.arc.properties.IfBuildProperty;
import jakarta.enterprise.context.ApplicationScoped;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -15,7 +16,11 @@ public final class MemoryStorageService extends LocalStorageServiceBase {
private final Map<String, byte[]> storage = new HashMap<>();

@Override
public byte[] fetch(final String path) {
public byte[] fetch(final String path) throws IOException {
if (!storage.containsKey(path)) {
throw new IOException();
}

return storage.get(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.ws.rs.NotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -55,7 +54,7 @@ public byte[] fetch(final String path) throws IOException {
try {
return client.getObject(b -> b.bucket(config.bucket()).key(path)).readAllBytes();
} catch (final NoSuchKeyException e) {
throw new NotFoundException();
throw new IOException();
}
}

Expand Down

0 comments on commit 14ee5e4

Please sign in to comment.