Skip to content

Instantly share code, notes, and snippets.

@steve9001
Created September 25, 2012 19:28
Show Gist options
  • Save steve9001/3783926 to your computer and use it in GitHub Desktop.
Save steve9001/3783926 to your computer and use it in GitHub Desktop.
class Exception
def pretty
trace = backtrace.select{ |l|l.start_with?(Rails.root.to_s) }.join("\n ")
"#{self.class}\n#{message}\n#{trace}\n"
end
end
@wm
Copy link

wm commented Sep 25, 2012

That is nice. So you would then call e.pretty instead?

Could you do this instead?

class PrettyException < SimpleDelegator
  def to_s
     trace = backtrace.select{ |l|l.start_with?(Rails.root.to_s) }.join("\n    ")
    "#{self.class}\n#{message}\n#{trace}\n"
  end
end

PrettyException.new(e)

@steve9001
Copy link
Author

Well by putting it on Exception you can use it in arbitrary exception handling:

task :do_stuff do
  User.all.each do |user|
    begin
      user.wow!
    rescue e
      Airbrake.notify "User id: #{user.id}\n#{e.pretty}"
    end
  end
end

@wm
Copy link

wm commented Sep 25, 2012

Right, or

task :do_stuff do
  User.all.each do |user|
    begin
      user.wow!
    rescue e
      Airbrake.notify "User id: #{user.id}\n#{PrettyException.new(e)}"
    end
  end
end

@steve9001
Copy link
Author

Ah I get it. Yes you could I think. Personally I'm not always opposed to adding methods to an object, so e.pretty would be fine with me. I haven't used SImpleDelegator before though, that's useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment