Skip to content
This repository has been archived by the owner on Mar 1, 2020. It is now read-only.

Commit

Permalink
add some nice methods to the exception object
Browse files Browse the repository at this point in the history
  • Loading branch information
maxjacobson committed Jan 7, 2016
1 parent 5e4909a commit e98c260
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/shell_whisperer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

module ShellWhisperer
class CommandFailed < StandardError
attr_reader :original_message, :original_command, :exit_code

def initialize(cmd, result, exit_code)
@original_command = cmd
@original_message = result
@exit_code = exit_code

super "Attempted to run #{cmd.inspect} but the shell reported error: " \
"#{result.chomp.inspect} and exited with exit code #{exit_code}."
end
Expand Down
14 changes: 14 additions & 0 deletions spec/shell_whisperer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
it "should fail" do
expect { subject }.to raise_error(ShellWhisperer::CommandFailed, /fafa/)
end

it "should expose information about what went wrong" do
failed = false
begin
subject
rescue ShellWhisperer::CommandFailed => e
expect(e.original_command).to eq 'fafa'
expect(e.original_message).to match /fafa/
expect(e.exit_code).to eq 127
failed = true
end

expect(failed).to be true
end
end
end
end

0 comments on commit e98c260

Please sign in to comment.