From b75a672ce2098701502564c9bcb2bd18ca5414b3 Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Tue, 25 Sep 2018 17:18:11 -0500 Subject: [PATCH] handling more errors gracefully --- Ficdown.Parser/Parser/BlockHandler.cs | 7 +++++-- Ficdown.Parser/Player/StateManager.cs | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Ficdown.Parser/Parser/BlockHandler.cs b/Ficdown.Parser/Parser/BlockHandler.cs index c1bda26..f8e202d 100644 --- a/Ficdown.Parser/Parser/BlockHandler.cs +++ b/Ficdown.Parser/Parser/BlockHandler.cs @@ -45,8 +45,11 @@ public Story ParseBlocks(IEnumerable blocks) { // get the story - var storyBlock = blocks.SingleOrDefault(b => b.Type == BlockType.Story); - if(storyBlock == null) throw new FicdownException("No story block found"); + var storyBlocks = blocks.Where(b => b.Type == BlockType.Story); + if(storyBlocks.Count() == 0) throw new FicdownException("No story block found"); + if(storyBlocks.Count() > 1) throw new FicdownException("More than one story block found"); + + var storyBlock = storyBlocks.Single(); Anchor storyAnchor; try diff --git a/Ficdown.Parser/Player/StateManager.cs b/Ficdown.Parser/Player/StateManager.cs index 23805b2..0722fd3 100644 --- a/Ficdown.Parser/Player/StateManager.cs +++ b/Ficdown.Parser/Player/StateManager.cs @@ -44,7 +44,7 @@ get { var scene = _story.Scenes[_story.FirstScene].Where(s => s.Conditions == null); - if(scene == null) + if(scene.Count() == 0) throw new FicdownException(_story.Name, string.Format("Story links to undefined scene: {0}", _story.FirstScene)); if(scene.Count() > 1) throw new FicdownException(_story.Name, string.Format("Story links to scene that is defined more than once: {0}", _story.FirstScene));