From f89f31ff4926b24f9440e19b7ee289fd37119a22 Mon Sep 17 00:00:00 2001 From: John Mair Date: Wed, 14 Sep 2011 23:35:08 +1200 Subject: [PATCH] added error handling to cat command --- CHANGELOG | 1 + bin/pry | 0 lib/pry/default_commands/shell.rb | 11 ++++++++--- test/test_default_commands/test_shell.rb | 6 ++++++ 4 files changed, 15 insertions(+), 3 deletions(-) mode change 100644 => 100755 bin/pry diff --git a/CHANGELOG b/CHANGELOG index 7cb4a4ab..7bc80f32 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,6 +12,7 @@ * added "unindent" helper to make adding help to commands easier * local ./.pryrc now loaded after ~/.pryrc if it exists * cat --ex N and edit --ex N now can navigate through backtrace, where cat --ex (with no args) moves throuh successive levels of the backtrace automatically with state stored on the exceptino object itself +* new option Pry.config.exception_window_size determines window size for cat --ex 8/9/2011 version 0.9.5 diff --git a/bin/pry b/bin/pry old mode 100644 new mode 100755 diff --git a/lib/pry/default_commands/shell.rb b/lib/pry/default_commands/shell.rb index 6e3fad17..b771be9a 100644 --- a/lib/pry/default_commands/shell.rb +++ b/lib/pry/default_commands/shell.rb @@ -92,8 +92,13 @@ class Pry next end - contents, _, _ = read_between_the_lines(file_name, start_line, end_line) - contents = syntax_highlight_by_file_type_or_specified(contents, file_name, opts[:type]) + begin + contents, _, _ = read_between_the_lines(file_name, start_line, end_line) + contents = syntax_highlight_by_file_type_or_specified(contents, file_name, opts[:type]) + rescue Errno::ENOENT + output.puts "Could not find file: #{file_name}" + next + end if opts.l? contents = text.with_line_numbers contents, start_line + 1 @@ -115,7 +120,7 @@ class Pry # header for exceptions output.puts "\n#{Pry::Helpers::Text.bold('Exception:')} #{_pry_.last_exception.class}: #{_pry_.last_exception.message}\n--" - output.puts "#{Pry::Helpers::Text.bold('From:')} #{ex_file} @ line #{ex_line} @ #{text.bold('level: ')} #{bt_index} of backtrace.\n\n" + output.puts "#{Pry::Helpers::Text.bold('From:')} #{ex_file} @ line #{ex_line} @ #{text.bold('level: ')} #{bt_index} of backtrace (of #{_pry_.last_exception.backtrace.size - 1}).\n\n" end set_file_and_dir_locals(file_name) diff --git a/test/test_default_commands/test_shell.rb b/test/test_default_commands/test_shell.rb index 1e584ac4..06d7cd23 100644 --- a/test/test_default_commands/test_shell.rb +++ b/test/test_default_commands/test_shell.rb @@ -3,6 +3,12 @@ require 'helper' describe "Pry::DefaultCommands::Shell" do describe "cat" do + describe "on receiving a file that does not exist" do + it 'should display an error message' do + mock_pry("cat supercalifragilicious66").should =~ /Could not find file/ + end + end + # this doesnt work so well on rbx due to differences in backtrace # so we currently skip rbx until we figure out a workaround describe "with --ex" do