Skip to content

Commit

Permalink
put remote exceptions into original_exception instead of dangerous ba…
Browse files Browse the repository at this point in the history
…cktrace hacking
  • Loading branch information
mojombo committed Sep 26, 2009
1 parent d46d97b commit c98fbc8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
29 changes: 15 additions & 14 deletions lib/bertrpc/errors.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
module BERTRPC
class BERTRPCError < StandardError
attr_accessor :code

def initialize(msg = nil, klass = "Error", bt = [])
@bt = bt
attr_accessor :code, :original_exception

def initialize(msg = nil, klass = nil, bt = [])
case msg
when Array
self.code = msg[0]
super("#{klass}: #{msg[1]}")
when String
self.code = 0
super("#{klass}: #{msg}")
code, message = msg
else
super("#{klass}: #{msg}")
code, message = [0, msg]
end

if klass
self.original_exception = RemoteError.new("#{klass}: #{message}")
self.original_exception.set_backtrace(bt)
end
end

def backtrace
x = super
x ? ['---REMOTE---'] + @bt + ['---LOCAL---'] + x : x
self.code = code
super(message)
end
end

class RemoteError < StandardError

end

class ConnectionError < BERTRPCError

end
Expand Down
24 changes: 22 additions & 2 deletions test/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@

class ErrorTest < Test::Unit::TestCase
context "Errors in general" do
should "concatenate remote and local backtraces" do
should "be creatable with just a message string" do
begin
raise BERTRPC::BERTRPCError.new('msg')
rescue Object => e
assert_equal "msg", e.message
assert_equal 0, e.code
end
end

should "be creatable with a [code, message] array" do
begin
raise BERTRPC::BERTRPCError.new([7, 'msg'])
rescue Object => e
assert_equal "msg", e.message
assert_equal 7, e.code
end
end

should "record the original exception" do
begin
raise BERTRPC::BERTRPCError.new('msg', 'Error', ['foo', 'bar'])
rescue Object => e
assert_equal ['---REMOTE---', 'foo', 'bar', '---LOCAL---'], e.backtrace[0..3]
assert_equal "msg", e.message
assert_equal "Error: msg", e.original_exception.message
assert_equal ['foo', 'bar'], e.original_exception.backtrace
end
end
end
Expand Down

0 comments on commit c98fbc8

Please sign in to comment.