Skip to content

Instantly share code, notes, and snippets.

@copiousfreetime
Created February 19, 2015 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save copiousfreetime/2fa6f5efd701399994c4 to your computer and use it in GitHub Desktop.
Save copiousfreetime/2fa6f5efd701399994c4 to your computer and use it in GitHub Desktop.
Ruby's Error Tree
#!/usr/bin/env ruby
# A more general case of finding all of the classes in a hierarchy
#
# Inspired by Nick Sieger's excellent article
# http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy/
#
top_class = Exception
prune_below = [ SystemCallError ]
outer_modules_to_skip = %w[ Gem ]
tree = {}
ObjectSpace.each_object(Class) do |cls|
next unless cls <= top_class
next if prune_below.any? { |parent| cls < parent }
outer_module = cls.name.split("::").first
next if outer_modules_to_skip.include?(outer_module)
ancestors = cls.ancestors.take_while { |a| a <= top_class }.reverse
ancestors.inject(tree) { |memo, k| memo[k] ||= {} }
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.(t[k]); indent -= 2
end
end
tree_printer.(tree)
Exception
MonitorMixin::ConditionVariable::Timeout
NoMemoryError
ScriptError
LoadError
NotImplementedError
SyntaxError
SecurityError
SignalException
Interrupt
StandardError
ArgumentError
EncodingError
Encoding::CompatibilityError
Encoding::ConverterNotFoundError
Encoding::InvalidByteSequenceError
Encoding::UndefinedConversionError
FiberError
IOError
EOFError
IndexError
KeyError
StopIteration
LocalJumpError
Math::DomainError
NameError
NoMethodError
RangeError
FloatDomainError
RegexpError
RuntimeError
SystemCallError
ThreadError
TypeError
ZeroDivisionError
SystemExit
SystemStackError
fatal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment