Skip to content

Instantly share code, notes, and snippets.

@arnab
Created February 23, 2012 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnab/1893924 to your computer and use it in GitHub Desktop.
Save arnab/1893924 to your computer and use it in GitHub Desktop.
Show available errors in a Ruby environment (so you know to raise the right error)
# From http://weblog.jamisbuck.org/2007/3/7/raising-the-right-exception
exceptions = []
tree = {}
ObjectSpace.each_object(Class) do |cls|
next unless cls.ancestors.include? Exception
next if exceptions.include? cls
next if cls.superclass == SystemCallError # avoid dumping Errno's
exceptions << cls
cls.ancestors.delete_if {|e| [Object, Kernel].include? e }.reverse.inject(tree) {|memo,cls| memo[cls] ||= {}}
end
indent = 0
tree_printer = Proc.new do |t|
t.keys.sort { |c1,c2| c1.name <=> c2.name }.each do |k|
space = (' ' * indent); space ||= ''
puts space + k.to_s
indent += 2; tree_printer.call t[k]; indent -= 2
end
end
tree_printer.call tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment