Permalink
Browse files

Spiffier Errors (internally + externally)

  • Loading branch information...
1 parent 072cf87 commit 776427f6fa5e25ba9a323014a112f0799b6b8728 @rking rking committed with rking@sharpsaw.org Dec 10, 2012
Showing with 17 additions and 9 deletions.
  1. +4 −0 Rakefile
  2. +12 −8 lib/jist.rb
  3. +1 −1 spec/clipboard_spec.rb
View
@@ -4,3 +4,7 @@ desc 'run the tests' # that's non-DRY
task :test do
sh 'rspec spec'
end
+
+task :clipfailtest do
+ sh 'PATH=/ /usr/bin/ruby -Ilib -S bin/jist -ac < lib/jist.rb'
+end
View
@@ -7,7 +7,7 @@
module Jist
extend self
- VERSION = '1.2.0'
+ VERSION = '1.3.0'
# A list of clipboard commands with copy and paste support.
CLIPBOARD_COMMANDS = {
@@ -22,6 +22,7 @@ module Jist
# Exception tag for errors raised while gisting.
module Error; end
+ class ClipboardError < RuntimeError; include Error end
# Upload a gist to https://gist.github.com
#
@@ -223,19 +224,19 @@ def on_success(body, options={})
# Copy a string to the clipboard.
#
# @param [String] content
- # @raise [RuntimeError] if no clipboard integration could be found
+ # @raise [Jist::Error] if no clipboard integration could be found
#
- # This method was heavily inspired by defunkt's Gist#copy,
- # @see https://github.com/defunkt/gist/blob/bca9b29/lib/gist.rb#L178
def copy(content)
IO.popen(clipboard_command(:copy), 'r+') { |clip| clip.print content }
- raise "Copying to clipboard failed" unless paste == content
+ raise Error, 'Copying to clipboard failed.' unless paste == content
+ rescue Error => e
+ raise ClipboardError, e.message + "\nAttempted to copy: #{content}"
end
# Get a string from the clipboard.
#
# @param [String] content
- # @raise [RuntimeError] if no clipboard integration could be found
+ # @raise [Jist::Error] if no clipboard integration could be found
def paste
`#{clipboard_command(:paste)}`
end
@@ -261,12 +262,15 @@ def which(cmd, path=ENV['PATH'])
#
# @param [Symbol] action either :copy or :paste
# @return [String] the command to run
- # @raise [RuntimeError] if no clipboard integration could be found
+ # @raise [Jist::ClipboardError] if no clipboard integration could be found
def clipboard_command(action)
command = CLIPBOARD_COMMANDS.keys.detect do |cmd|
which cmd
end
- raise "Could not find copy command, tried: #{CLIPBOARD_COMMANDS}" unless command
+ raise ClipboardError, <<-EOT unless command
+Could not find copy command, tried:
+ #{CLIPBOARD_COMMANDS.values.join(' || ')}
+ EOT
action == :copy ? command : CLIPBOARD_COMMANDS[command]
end
View
@@ -29,6 +29,6 @@ def jist_but_dont_ask_for_copy
ENV['PATH'] = ''
lambda{
ask_for_copy
- }.should raise_error(/Could not find copy command/)
+ }.should raise_error(/Could not find copy command.*http/m)
end
end

0 comments on commit 776427f

Please sign in to comment.