Skip to content

Commit

Permalink
Allow NoSuchFileException to indicate missing
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Dec 12, 2024
1 parent 2619007 commit e1e07d2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
Expand Down Expand Up @@ -216,7 +217,7 @@ KeyValues load(Node node, InternalKeyValuesResource resource, Set<LoadFlag> flag
catch (KeyValuesException e) {
throw new IOException("Resource has key value errors. resource: " + describe(node), e);
}
catch (FileNotFoundException e) {
catch (FileNotFoundException | NoSuchFileException e) {
logger.missing(resource, e);
if (LoadFlag.NO_REQUIRE.isSet(flags)) {
return KeyValues.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ default void loaded(KeyValuesResource resource) {
* @param resource the resource that was not found
* @param exception the exception that occurred when the resource was not found
*/
default void missing(KeyValuesResource resource, FileNotFoundException exception) {
default void missing(KeyValuesResource resource, Exception exception) {
debug(KeyValueReference.describe(new StringBuilder("Missing "), resource, false).toString());
}

Expand Down Expand Up @@ -294,7 +294,7 @@ public void loaded(KeyValuesResource resource) {
}

@Override
public void missing(KeyValuesResource resource, FileNotFoundException exception) {
public void missing(KeyValuesResource resource, Exception exception) {
}

}
Expand Down
10 changes: 8 additions & 2 deletions ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
Expand Down Expand Up @@ -150,10 +151,15 @@ public KeyValuesLoader build() {
* Loads key-values using the current builder configuration.
* @return a {@link KeyValues} instance containing the loaded key-value pairs
* @throws IOException if an I/O error occurs during loading
* @throws FileNotFoundException if a specified resource is not found
* @throws FileNotFoundException if a specified resource is not found. This
* exception gets special treatment if thrown if the resource is optional it will
* not be an error.
* @throws NoSuchFileException if a specified resource is not found. This
* exception gets special treatment if thrown if the resource is optional it will
* not be an error.
*/
@Override
public KeyValues load() throws IOException, FileNotFoundException {
public KeyValues load() throws IOException, FileNotFoundException, NoSuchFileException {
return build().load();
}

Expand Down
14 changes: 12 additions & 2 deletions ezkv-kvs/src/main/java/io/jstach/ezkv/kvs/KeyValuesResource.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.jstach.ezkv.kvs;

import java.io.FileNotFoundException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.EnumSet;
Expand Down Expand Up @@ -210,7 +212,13 @@ public sealed interface KeyValuesResource extends NamedKeyValuesSource, KeyValue

/**
* Indicates that the resource is optional and no error should occur if it is not
* found.
* found. {@linkplain KeyValuesLoader Loaders} can indicate not found by throwing
* either {@link FileNotFoundException} or {@link NoSuchFileException}. If the
* resource is missing during a full load it will be logged with
* {@link KeyValuesEnvironment.Logger#missing(KeyValuesResource, Exception)}. Note
* that a resource may just not have any key values but is still found. By default
* resources are normally required to exist.
* @see #FLAG_NO_EMPTY
*/
public static final String FLAG_NO_REQUIRE = "NO_REQUIRE";

Expand All @@ -226,7 +234,9 @@ public sealed interface KeyValuesResource extends NamedKeyValuesSource, KeyValue

/**
* Indicates that the resource should not be empty. Typically used to enforce that the
* resource contains at least one key-value pair.
* resource contains at least one key-value pair. This is different than
* {@link #FLAG_NO_REQUIRE} which is if the resource does not exist at all. By default
* empty resources are normally allowed.
*/
public static final String FLAG_NO_EMPTY = "NO_EMPTY";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.FileNotFoundException;
import java.net.URI;
import java.nio.file.NoSuchFileException;
import java.util.Optional;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -103,9 +104,9 @@ default KeyValuesMedia.Parser requireParser(KeyValuesResource resource) throws K
* Finds a {@link KeyValuesLoader} capable of loading the specified
* {@link KeyValuesResource} using the provided {@link LoaderContext}. <strong>If
* the resource cannot be found the returned {@link KeyValuesLoader} should throw
* a {@link FileNotFoundException} to indicate not found and not Optional.empty
* which conversely means no matching loader could be found based on the resource.
* </strong>
* a {@link FileNotFoundException} or {@link NoSuchFileException} to indicate not
* found and not Optional.empty which conversely means no matching loader could be
* found based on the resource. </strong>
* @param context the context containing dependencies and services
* @param resource the resource for which a loader is sought
* @return an {@link Optional} containing the loader if found, or empty if not
Expand Down

0 comments on commit e1e07d2

Please sign in to comment.