From c7a80df29dac0cb5a7b5bd78a750c15fd6146908 Mon Sep 17 00:00:00 2001 From: a14m Date: Sun, 6 Dec 2020 16:16:35 +0100 Subject: Add the error handling for missing/invalid file/URL input --- CLI/ruby/lib/game_of_life/generators/input.rb | 5 +++++ .../spec/game_of_life/generators/input_spec.rb | 24 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CLI/ruby/lib/game_of_life/generators/input.rb b/CLI/ruby/lib/game_of_life/generators/input.rb index c0d96c1..3082e70 100644 --- a/CLI/ruby/lib/game_of_life/generators/input.rb +++ b/CLI/ruby/lib/game_of_life/generators/input.rb @@ -22,6 +22,11 @@ module GameOfLife # where any alpha numeric character is considered a living cell (true) raw.map! { |row| row.chars.map { |char| char.match?(/[[:alnum:]]/) } } populate(parsed_input: raw, width: options["width"].to_i, height: options["height"].to_i) + rescue OpenURI::HTTPError, SocketError + raise GameOfLife::Error, "URL isn't avaialable, please check it again and make sure the URL is correct" + rescue Errno::ENOENT + raise GameOfLife::Error, + "File isn't avaialable, please check it again and make sure the path to the input file is correct" end # Populate the {GameOfLife::Universe} with reference to the parsed input 2D array (True/False) diff --git a/CLI/ruby/spec/game_of_life/generators/input_spec.rb b/CLI/ruby/spec/game_of_life/generators/input_spec.rb index e05dbfb..6986787 100644 --- a/CLI/ruby/spec/game_of_life/generators/input_spec.rb +++ b/CLI/ruby/spec/game_of_life/generators/input_spec.rb @@ -51,5 +51,29 @@ RSpec.describe GameOfLife::Generators::Input do expect(described_class.new(options).to_s).to eq universe end + + it "rescues Errno::ENOENT and raises GameOfLife::Error" do + options = { "input" => "not-existing-file.txt", "width" => 25, "height" => 2 } + expect { described_class.new(options) }.to raise_error( + GameOfLife::Error, + "File isn't avaialable, please check it again and make sure the path to the input file is correct", + ) + end + + it "rescues SocketError and raises GameOfLife::Error" do + options = { "input" => "https://not-existing-url.com", "width" => 25, "height" => 2 } + expect { described_class.new(options) }.to raise_error( + GameOfLife::Error, + "URL isn't avaialable, please check it again and make sure the URL is correct", + ) + end + + it "rescues OpenURI::HTTPError and raises GameOfLife::Error" do + options = { "input" => "https://example.com/non-existing", "width" => 25, "height" => 2 } + expect { described_class.new(options) }.to raise_error( + GameOfLife::Error, + "URL isn't avaialable, please check it again and make sure the URL is correct", + ) + end end end -- cgit v1.2.3