Skip to content

Commit

Permalink
When we fail due to multiple strategies failing, add the error as sup…
Browse files Browse the repository at this point in the history
…pressed exceptions (#96)
  • Loading branch information
j-baker authored and ellisjoe committed Oct 25, 2017
1 parent c433693 commit 9ba05fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -57,16 +58,20 @@ public CompletableFuture<Void> put(String fileKey, KeyMaterial keyMaterial) {
@Override
public CompletableFuture<KeyMaterial> get(String fileKey) {
return CompletableFuture.supplyAsync(() -> {
List<Exception> suppressedExceptions = new ArrayList<>();
for (AsyncKeyStorageStrategy strategy : strategies) {
try {
return strategy.get(fileKey).join();
} catch (Exception e) {
suppressedExceptions.add(e);
log.info("Failed to get key material using {}", strategy.getClass().getCanonicalName(), e);
}
}
throw new RuntimeException(String.format(
RuntimeException toThrow = new RuntimeException(String.format(
"Unable to get key material using any of the provided strategies: %s",
Collections2.transform(strategies, s -> s.getClass().getCanonicalName())));
suppressedExceptions.forEach(toThrow::addSuppressed);
throw toThrow;
}, executor);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
Expand Down Expand Up @@ -53,17 +54,21 @@ public void put(String fileKey, KeyMaterial keyMaterial) {

@Override
public KeyMaterial get(String fileKey) {
List<Exception> suppressedExceptions = new ArrayList<>();
for (KeyStorageStrategy strategy : strategies) {
try {
return strategy.get(fileKey);
} catch (Exception e) {
suppressedExceptions.add(e);
logger.info("Failed to get key material using {}", strategy.getClass().getCanonicalName(), e);
}
}
throw new RuntimeException(String.format(
RuntimeException toThrow = new RuntimeException(String.format(
"Unable to get key material for '%s' using any of the provided strategies: %s",
fileKey,
Collections2.transform(strategies, s -> s.getClass().getCanonicalName())));
suppressedExceptions.stream().forEach(toThrow::addSuppressed);
throw toThrow;
}

@Override
Expand Down

0 comments on commit 9ba05fe

Please sign in to comment.