Skip to content

Commit

Permalink
Better error handling of invalid URI's
Browse files Browse the repository at this point in the history
- fixes #54
- normalizes error handling of invalid command line options
- wraps all Launchy.open exceptions in a Launchy::Errorjj
  • Loading branch information
copiousfreetime committed Feb 5, 2013
1 parent b0c7319 commit 99c7426
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
1 change: 1 addition & 0 deletions HISTORY.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

* Chagne XFCE detection to not depend on grep (copiousfreetime/launchy#52 - thanks bogdan)
* Suppress forked process output (copiousfreetime/launchy#51)
* Display help/usage if no url is given (copiousfreetime/launchy#54)

== Version 2.1.2 - 2012-08-06

Expand Down
18 changes: 9 additions & 9 deletions lib/launchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class << self
def open(uri, options = {} )
begin
extract_global_options( options )
uri = Addressable::URI.parse( uri )
app = Launchy::Application.handling( uri )
app.new.open( uri, options )
a_uri = Addressable::URI.parse( uri )
raise Launchy::ArgumentError, "Invalid URI given: #{uri.inspect}" unless a_uri

app = Launchy::Application.handling( a_uri )
app.new.open( a_uri, options )
rescue Launchy::Error => le
raise le
rescue Exception => e
msg = "Failure in opening #{uri} with options #{options.inspect}: #{e}"
Launchy.log "#{self.name} : #{msg}"
e.backtrace.each do |bt|
Launchy.log bt
end
$stderr.puts msg
msg = "Failure in opening uri #{uri.inspect} with options #{options.inspect}: #{e}"
raise Launchy::Error, msg
end
end

Expand Down
24 changes: 16 additions & 8 deletions lib/launchy/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,10 @@ def parser
end

def parse( argv, env )
begin
parser.parse!( argv )
return true
rescue ::OptionParser::ParseError => pe
$stderr.puts "#{parser.program_name}: #{pe}"
$stderr.puts "Try `#{parser.program_name} --help for more information."
return false
end
parser.parse!( argv )
return true
rescue ::OptionParser::ParseError => pe
error_output( pe )
end

def good_run( argv, env )
Expand All @@ -74,6 +70,18 @@ def good_run( argv, env )
else
return false
end
rescue StandardError => e
error_output( e )
end

def error_output( error )
$stderr.puts "ERROR: #{error}"
Launchy.log "ERROR: #{error}"
error.backtrace.each do |bt|
Launchy.log bt
end
$stderr.puts "Try `#{parser.program_name} --help' for more information."
return false
end

def run( argv = ARGV, env = ENV )
Expand Down
1 change: 1 addition & 0 deletions lib/launchy/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module Launchy
class Error < ::StandardError; end
class ApplicationNotFoundError < Error; end
class CommandNotFoundError < Error; end
class ArgumentError < Error; end
end
5 changes: 2 additions & 3 deletions spec/launchy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@
Launchy.ruby_engine.must_equal 'myruby'
end

it "prints an error on stderr when no scheme is found for the given uri" do
Launchy.open( "blah://something/invalid" )
$stderr.string.must_match( /Failure in opening/ )
it "raises an exception if no scheme is found for the given uri" do
lambda { Launchy.open( "blah://something/invalid" ) }.must_raise Launchy::ApplicationNotFoundError
end

end

0 comments on commit 99c7426

Please sign in to comment.