Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
* Explain that the error handling is being skipped when GLI_DEBUG is on and error handler returns false
* Do not call the error handler on help requests

Fixes #150
Fixes #151
  • Loading branch information
davetron5000 committed Jul 6, 2013
1 parent 9c1bc10 commit 7cbcd29
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/gli/app_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ def handle_exception(ex,command)
commands[:help].execute({},{},command.nil? ? [] : [command.name.to_s])
end
end
elsif ENV['GLI_DEBUG'] == 'true'
stderr.puts "Custom error handler exited false, skipping normal error handling"
end

raise ex if ENV['GLI_DEBUG'] == 'true'
Expand Down Expand Up @@ -254,7 +256,8 @@ def proceed?(parsing_result) #:nodoc:
# Returns true if we should proceed with GLI's basic error handling.
# This calls the error block if the user provided one
def regular_error_handling?(ex) #:nodoc:
if @error_block
if @error_block
return true if (ex.respond_to?(:exit_code) && ex.exit_code == 0)
@error_block.call(ex)
else
true
Expand Down
42 changes: 42 additions & 0 deletions test/tc_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,48 @@ def test_around_filter_handles_help_now
assert_contained(@fake_stdout,/SYNOPSIS/)
end

def test_error_handler_prints_that_its_skipping_when_gli_debug_is_set
ENV["GLI_DEBUG"] = 'true'
@app.on_error do
false
end
@app.command :blah do |c|
c.action do |*|
raise 'wtf'
end
end

assert_raises(RuntimeError) {
@app.run(['blah'])
}
assert_contained(@fake_stderr,/Custom error handler exited false, skipping normal error handling/)
end

def test_error_handler_should_be_called_on_help_now
@app.command :blah do |c|
c.action do |*|
help_now!
end
end
@app.run(["blah"])
assert @error_called
end

def test_error_handler_shouldnt_be_called_on_help_from_command_line
@app.command :blah do |c|
c.action do |*|
end
end
[
["--help", "blah"],
["blah", "--help"],
].each do |args|
args_copy = args.clone
@app.run(args)
assert !@error_called, "for args #{args_copy.inspect}"
end
end

def test_command_skips_pre
@app.skips_pre
@app.skips_post
Expand Down

0 comments on commit 7cbcd29

Please sign in to comment.