From 99c7426a59fcde692bce3de15df8c3c6b89675eb Mon Sep 17 00:00:00 2001 From: Jeremy Hinegardner Date: Tue, 5 Feb 2013 16:29:01 +0800 Subject: [PATCH] Better error handling of invalid URI's - fixes #54 - normalizes error handling of invalid command line options - wraps all Launchy.open exceptions in a Launchy::Errorjj --- HISTORY.rdoc | 1 + lib/launchy.rb | 18 +++++++++--------- lib/launchy/cli.rb | 24 ++++++++++++++++-------- lib/launchy/error.rb | 1 + spec/launchy_spec.rb | 5 ++--- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/HISTORY.rdoc b/HISTORY.rdoc index 857d284..3814740 100644 --- a/HISTORY.rdoc +++ b/HISTORY.rdoc @@ -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 diff --git a/lib/launchy.rb b/lib/launchy.rb index d4197d1..cb82834 100644 --- a/lib/launchy.rb +++ b/lib/launchy.rb @@ -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 diff --git a/lib/launchy/cli.rb b/lib/launchy/cli.rb index 87d0140..3b365e4 100644 --- a/lib/launchy/cli.rb +++ b/lib/launchy/cli.rb @@ -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 ) @@ -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 ) diff --git a/lib/launchy/error.rb b/lib/launchy/error.rb index 2c594c5..fc1dda9 100644 --- a/lib/launchy/error.rb +++ b/lib/launchy/error.rb @@ -2,4 +2,5 @@ module Launchy class Error < ::StandardError; end class ApplicationNotFoundError < Error; end class CommandNotFoundError < Error; end + class ArgumentError < Error; end end diff --git a/spec/launchy_spec.rb b/spec/launchy_spec.rb index 4f2e761..349316f 100644 --- a/spec/launchy_spec.rb +++ b/spec/launchy_spec.rb @@ -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