Browse files

explain the error conversion hairball

  • Loading branch information...
1 parent 9065e65 commit bb42a8b8bdc94a9ce736273d61c9e0486b4b5b06 @cowboyd committed Aug 13, 2012
Showing with 34 additions and 2 deletions.
  1. +34 −2 lib/v8/error.rb
View
36 lib/v8/error.rb
@@ -103,6 +103,40 @@ def protect
end
+ # Convert the result of a triggered JavaScript try/catch block into
+ # a V8::Error
+ #
+ # This is a bit of a yak-shave because JavaScript let's you throw all
+ # kinds of things. We do our best to make sure that the message property
+ # of the resulting V8::Error is as helpful as possible, and that it
+ # contains as much source location information as we can put onto it.
+ #
+ # For example:
+ #
+ # throw 4
+ # throw 'four'
+ # throw {number: 4}
+ #
+ # are all valid cases, none of which actually reference an exception object
+ # with a stack trace and a message. only with something like:
+ #
+ # throw new Error('fail!')
+ #
+ # do you get the a proper stacktrace and a message property. However a lot of
+ # times JavaScript library authors are lazy and do this:
+ #
+ # throw {message: 'foo', otherMetadata: 'bar'}
+ #
+ # It's common enough so we do the courtesy of having the resulting V8::Error
+ # have as its message in ruby land the 'message' property of the value object
+ #
+ # To further complicate things, SyntaxErrors do not have a JavaScript stack
+ # (even if they occur during js execution). This can make debugging a nightmare
+ # so we copy in the source location of the syntax error into the message of
+ # the resulting V8::Error
+ #
+ # @param [V8::C::TryCatch] native trycatch object that has been triggered
+ # @return [V8::Error] the error generated by this try/catch
def self.Error(trycatch)
exception = trycatch.Exception()
value = exception.to_ruby
@@ -114,8 +148,6 @@ def self.Error(trycatch)
if cause = exception.GetHiddenValue("rr::Cause")
cause = cause.Value()
end
- # SyntaxErrors do not have a JavaScript stack (even if they occur during js execution).
- # To caputre where the error occured, we need to put it in the message
if value['constructor'] == V8::Context.current['SyntaxError']
info = trycatch.Message()
resource_name = info.GetScriptResourceName().to_ruby

0 comments on commit bb42a8b

Please sign in to comment.