Skip to content

Commit

Permalink
Improve error handling for savegame info failures
Browse files Browse the repository at this point in the history
  • Loading branch information
crschnick committed May 24, 2023
1 parent 98baafa commit 0126bfd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
Expand All @@ -62,7 +63,7 @@ public abstract class SavegameStorage<
public static final BidiMap<Game, SavegameStorage<?, ?>> ALL = new DualHashBidiMap<>();
private final Logger logger;
private final Class<I> infoClass;
private final FailableBiFunction<SavegameContent, Boolean, I, Exception> infoFactory;
private final FailableBiFunction<SavegameContent, Boolean, I, Throwable> infoFactory;
private final String name;
private final GameDateType dateType;
private final Path path;
Expand All @@ -75,9 +76,13 @@ public SavegameStorage(
SavegameType type,
Class<I> infoClass) {
this.infoFactory = (content, melted) -> {
try {
var created = (I) infoClass.getDeclaredConstructor(SavegameContent.class).newInstance(content);
created.getData().setBinary(melted);
return created;
} catch (InvocationTargetException ex) {
throw ex.getCause();
}
};
this.name = name;
this.type = type;
Expand Down Expand Up @@ -583,7 +588,7 @@ public synchronized void melt(SavegameEntry<T,I> e) {
var name = getSavegameCampaign(e).getName() + " (" + PdxuI18n.get("MELTED") + ")";
addEntryToCollection(targetCollection, file -> struc.write(file, c), checksum, info, null, name);
saveData();
} catch (Exception ex) {
} catch (Throwable ex) {
ErrorHandler.handleException(ex);
}
}
Expand Down Expand Up @@ -686,7 +691,7 @@ public void success(SavegameParseResult.Success s) {
I info = null;
try {
info = infoFactory.apply(s.content, melted);
} catch (Exception e) {
} catch (Throwable e) {
resultToReturn[0] = new SavegameParseResult.Error(e);
return;
}
Expand Down Expand Up @@ -775,7 +780,7 @@ synchronized void createNewBranch(SavegameEntry<T,I> e) {
I info;
try {
info = infoFactory.apply(s.content, melted);
} catch (Exception ex) {
} catch (Throwable ex) {
ErrorHandler.handleException(ex);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.crschnick.pdxu.io.savegame;

import com.crschnick.pdxu.io.node.ArrayNode;
import com.crschnick.pdxu.io.node.LinkedArrayNode;
import com.crschnick.pdxu.io.node.Node;

import java.util.Map;
import java.util.Optional;

public abstract class SavegameParseResult {

public abstract void visit(Visitor visitor);

public abstract Success orThrow() throws Exception;
public abstract Success orThrow() throws Throwable;

public Optional<Success> success() {
return Optional.empty();
Expand Down Expand Up @@ -55,14 +52,14 @@ public Optional<Success> success() {

public static class Error extends SavegameParseResult {

public Exception error;
public Throwable error;

public Error(Exception error) {
public Error(Throwable error) {
this.error = error;
}

@Override
public Success orThrow() throws Exception {
public Success orThrow() throws Throwable {
throw error;
}

Expand Down

0 comments on commit 0126bfd

Please sign in to comment.