How to get error line number while executing ruby code. #2464
Replies: 4 comments
|
I don't think that's an easy problem to be solved, but hey, we may at least try. There's no mapping of the lines in the generated code to the lines of the source code. You can generate it alongside, it's called a source map. require 'opal/source_map'
c = Opal::Compiler.new("puts 'hello world'\nputs 'error world'+3\nputs 'unreachable'")
c.compile # Generates the code
c.source_map # Generates the source mapThe thing that we get out of that is an object of a class Opal::SourceMap::File. https://github.com/opal/opal/blob/master/lib/opal/source_map/file.rb Now, it's easy to generate a source map in a format understandable by browsers. Let's try to eval it this way: `eval(#{c.compile+"\n"+c.source_map.to_data_uri_comment})`This could give us some hint to what's happening, but it depends on an implementation to support that. You may get some info in a backtrace. But you may not. I get this backtrace on opal-repl (miniracer): The thing we are interested in is obviously: (Obviously for us, for your implementation it may not be that obvious). That line is: Ok, let's try some more on this source_map object. It has some methods that look promising enough to suspect that they contain the source map in a more unprocessed form: Ok, we are getting somewhere. Now, I don't know anything about this format, but let's try some more. We got a line 11 and character 14. Computers start counting characters from 0, so it's character 13. And this thing has 13: Was the original code on line 2 (1+1)? Yes. So, let's try that on a different example: It worked? Well it did :D. I know it may not be a solution you were looking for and maybe you think that this should be a part of the corelib and readable from an Exception object. We did as bit of hackery here and including this thing by default would increase the generated source codes (we would need to include the absolute_mappings along with the JS compiled code), which isn't what most Opal users want, but you can surely use it in your implementation. |
|
Thanks, @hmdne for a detailed response. I tried the same approach for the below code snippet but not able to get the exact line number. # Calculate grade using if and range
puts "John, how many questions you got right?"
answer = 30
if answer > 100
puts "That’s not possible"
end
if answer == 100
puts "You got A+ grade."
end
if (80..100).include? answer
puts "You got A grade"
end
if (60..81).include? answer
puts "You got B grade"
end
if (40..61).include? answer
puts "You got C grade"
end
if (20..41).include? answer
puts "You got D grade" + 4
end
if (0..21).include? answer
puts "You got E grade"
end
if answer == 0
puts "You got F grade"
endI got the below error. From the above error, I got 26 and 34 line numbers. Also, absolute mapping returns below array for 26-1. So for getting the exact error line number, I am not able to find an array that contains 34-1. Could you please help me with this. |
|
Of course. You need to take a line and character of a single backtrace entry into consideration. It's Therefore, it's this array that you are interested in, because it starts with 16-1: |
|
depending on the environment in which the code is run you can also try with https://github.com/evanw/node-source-map-support or other project that will sort out the mapping and backtrace for you… Another option is to inline the generated source at the end of the eval and let the browser do the work, using builder the code looks like this: This inlined sourcemap will also contain the full-body of the original ruby sources so it should be complete. |
Uh oh!
There was an error while loading. Please reload this page.
I am opal in my application but I am having a problem with getting an error line number while executing any code.
In a real scenario, we are getting this source code from a web-based code editor.
Please help to get the error line number.
We are building something similar to TryRuby.
All reactions