From 87323533af965dd8db070650d1c46f59bc6d202d Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Thu, 12 Mar 2015 01:18:50 -0400 Subject: [PATCH] Better error handling for Replay.load(). Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/replay/Replay.java | 17 +++++++---------- src/itdelatrisu/opsu/states/Game.java | 2 +- src/itdelatrisu/opsu/states/GameRanking.java | 19 +++++++++++++++---- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/itdelatrisu/opsu/replay/Replay.java b/src/itdelatrisu/opsu/replay/Replay.java index 327dfabb..db9877f4 100644 --- a/src/itdelatrisu/opsu/replay/Replay.java +++ b/src/itdelatrisu/opsu/replay/Replay.java @@ -118,20 +118,17 @@ public class Replay { /** * Loads the replay data. + * @throws IOException failure to load the data */ - public void load() { + public void load() throws IOException { if (loaded) return; - try { - OsuReader reader = new OsuReader(file); - loadHeader(reader); - loadData(reader); - reader.close(); - loaded = true; - } catch (IOException e) { - ErrorHandler.error("Could not load replay data.", e, true); - } + OsuReader reader = new OsuReader(file); + loadHeader(reader); + loadData(reader); + reader.close(); + loaded = true; } /** diff --git a/src/itdelatrisu/opsu/states/Game.java b/src/itdelatrisu/opsu/states/Game.java index 613cf610..78ff8bea 100644 --- a/src/itdelatrisu/opsu/states/Game.java +++ b/src/itdelatrisu/opsu/states/Game.java @@ -1186,7 +1186,7 @@ public class Game extends BasicGameState { this.replay = null; } else { if (replay.frames == null) { - ErrorHandler.error("Invalid replay.", null, false); + ErrorHandler.error("Attempting to set a replay with no frames.", null, false); return; } this.isReplay = true; diff --git a/src/itdelatrisu/opsu/states/GameRanking.java b/src/itdelatrisu/opsu/states/GameRanking.java index 27bc53b4..9f4f1de4 100644 --- a/src/itdelatrisu/opsu/states/GameRanking.java +++ b/src/itdelatrisu/opsu/states/GameRanking.java @@ -31,6 +31,9 @@ import itdelatrisu.opsu.audio.SoundController; import itdelatrisu.opsu.audio.SoundEffect; import itdelatrisu.opsu.replay.Replay; +import java.io.FileNotFoundException; +import java.io.IOException; + import org.lwjgl.opengl.Display; import org.newdawn.slick.Color; import org.newdawn.slick.GameContainer; @@ -42,6 +45,7 @@ import org.newdawn.slick.state.BasicGameState; import org.newdawn.slick.state.StateBasedGame; import org.newdawn.slick.state.transition.FadeInTransition; import org.newdawn.slick.state.transition.FadeOutTransition; +import org.newdawn.slick.util.Log; /** * "Game Ranking" (score card) state. @@ -168,10 +172,17 @@ public class GameRanking extends BasicGameState { if (replayButton.contains(x, y)) { Replay r = data.getReplay(null); if (r != null) { - r.load(); - gameState.setReplay(r); - gameState.setRestart((data.isGameplay()) ? Game.Restart.REPLAY : Game.Restart.NEW); - returnToGame = true; + try { + r.load(); + gameState.setReplay(r); + gameState.setRestart((data.isGameplay()) ? Game.Restart.REPLAY : Game.Restart.NEW); + returnToGame = true; + } catch (FileNotFoundException e) { + UI.sendBarNotification("Replay file not found."); + } catch (IOException e) { + Log.error("Failed to load replay data.", e); + UI.sendBarNotification("Failed to load replay data. See log for details."); + } } else UI.sendBarNotification("Replay file not found."); }