Skip to content

Commit

Permalink
Merge pull request #258 from deivid-rodriguez/more_friendly_errors
Browse files Browse the repository at this point in the history
More friendly errors
  • Loading branch information
David Rodríguez committed May 16, 2016
2 parents 4a2b884 + 89f937a commit 2b8bead
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 41 deletions.
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-05-10 10:05:38 +0200 using RuboCop version 0.39.0.
# on 2016-05-14 08:08:13 +0200 using RuboCop version 0.40.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 18
# Offense count: 17
Metrics/AbcSize:
Max: 28

Expand All @@ -19,7 +19,7 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 10

# Offense count: 59
# Offense count: 58
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 39
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Master (Unreleased)

### Fixed

* Unfriendly output in byebug's executable when no script specified (#256).
* Unfriendly output in byebug's executable when script doesn't exist.
* Unfriendly output in byebug's executable when script has invalid code.

## 9.0.2 - 2016-05-15

### Fixed
Expand Down
92 changes: 57 additions & 35 deletions lib/byebug/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ module Byebug
class Runner
include Helpers::ParseHelper

#
# Error class signaling absence of a script to debug.
#
class NoScript < StandardError; end

#
# Error class signaling a non existent script to debug.
#
class NonExistentScript < StandardError; end

#
# Error class signaling a script with invalid Ruby syntax.
#
class InvalidScript < StandardError; end

#
# Special working modes that don't actually start the debugger.
#
Expand Down Expand Up @@ -63,7 +48,7 @@ def initialize(stop = true, quit = true)
def help=(text)
@help ||= text

interface.puts("\n#{text}\n")
interface.puts("#{text}\n")
end

def version=(number)
Expand All @@ -74,6 +59,8 @@ def version=(number)

def remote=(host_and_port)
@remote ||= Byebug.parse_host_and_port(host_and_port)

Byebug.start_client(*@remote)
end

def init_script
Expand All @@ -97,18 +84,11 @@ def banner
# Starts byebug to debug a program.
#
def run
prepare_options.order!($ARGV)
return if version || help

if remote
Byebug.start_client(*remote)
return
end
option_parser.order!($ARGV)
return if non_script_option? || error_in_script?

Byebug.run_init_script if init_script

setup_cmd_line_args

loop do
debug_program

Expand All @@ -127,36 +107,70 @@ def interface
#
# Processes options passed from the command line.
#
def prepare_options
OptionParser.new(banner, 25) do |opts|
def option_parser
@option_parser ||= OptionParser.new(banner, 25) do |opts|
opts.banner = banner

OptionSetter.new(self, opts).setup
end
end

#
# An option that doesn't need a script specified was given
#
def non_script_option?
version || help || remote
end

#
# There is an error with the specified script
#
def error_in_script?
no_script? || non_existing_script? || invalid_script?
end

#
# No script to debug specified
#
def no_script?
return false unless $ARGV.empty?

print_error('You must specify a program to debug')
true
end

#
# Extracts debugged program from command line args.
#
def setup_cmd_line_args
def non_existing_script?
Byebug.mode = :standalone

raise(NoScript, 'You must specify a program to debug...') if $ARGV.empty?

program = which($ARGV.shift)
program = which($ARGV.shift) if program == which('ruby')
raise(NonExistentScript, "The script doesn't exist") unless program

$PROGRAM_NAME = program
if program
$PROGRAM_NAME = program
false
else
print_error("The script doesn't exist")
true
end
end

#
# Checks the debugged script has correct syntax
#
def invalid_script?
return false if syntax_valid?(File.read($PROGRAM_NAME))

print_error('The script has incorrect syntax')
true
end

#
# Debugs a script only if syntax checks okay.
#
def debug_program
ok = syntax_valid?(File.read($PROGRAM_NAME))
raise(InvalidScript, 'The script has incorrect syntax') unless ok

error = Byebug.debug_load($PROGRAM_NAME, stop)
puts "#{error}\n#{error.backtrace}" if error
end
Expand All @@ -178,5 +192,13 @@ def which(cmd)

nil
end

#
# Prints an error message and a help string
#
def print_error(msg)
interface.errmsg(msg)
interface.puts(option_parser.help)
end
end
end
12 changes: 9 additions & 3 deletions test/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ def test_run_with_remote_option_with_host_and_port_specification

def test_run_without_a_script_to_debug
with_command_line('bin/byebug') do
assert_raises(Runner::NoScript) { runner.run }
runner.run

check_error_includes 'You must specify a program to debug'
end
end

def test_run_with_an_nonexistent_script
with_command_line('bin/byebug', 'non_existent_script.rb') do
assert_raises(Runner::NonExistentScript) { runner.run }
runner.run

check_error_includes "The script doesn't exist"
end
end

Expand All @@ -62,7 +66,9 @@ def test_run_with_an_invalid_script
example_file.close

with_command_line('bin/byebug', example_path) do
assert_raises(Runner::InvalidScript) { runner.run }
runner.run

check_error_includes 'The script has incorrect syntax'
end
end

Expand Down

0 comments on commit 2b8bead

Please sign in to comment.