Delete JSON file in case of an error.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 17 Mar 2014 19:38:23 +0000 (20:38 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 17 Mar 2014 19:38:23 +0000 (20:38 +0100)
src/main/java/net/pterodactylus/rhynodge/states/StateManager.java

index 35125bb..a52e74d 100644 (file)
@@ -96,8 +96,9 @@ public class StateManager {
         *            The state to save
         */
        public void saveState(String reactionName, State state) {
+               File stateFile = null;
                try {
-                       File stateFile = stateFile(reactionName, "last");
+                       stateFile = stateFile(reactionName, "last");
                        objectMapper.writeValue(stateFile, state);
                        if (state.success()) {
                                stateFile = stateFile(reactionName, "success");
@@ -105,10 +106,13 @@ public class StateManager {
                        }
                } catch (JsonGenerationException jge1) {
                        logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jge1);
+                       stateFile.delete();
                } catch (JsonMappingException jme1) {
                        logger.warn(String.format("State for Reaction “%s” could not be generated.", reactionName), jme1);
+                       stateFile.delete();
                } catch (IOException ioe1) {
                        logger.warn(String.format("State for Reaction “%s” could not be written.", reactionName));
+                       stateFile.delete();
                }
        }