Skip to content

Instantly share code, notes, and snippets.

@jaredatron
Created August 3, 2015 19:24
Show Gist options
  • Save jaredatron/c1bcabc6fc7b8f8c8457 to your computer and use it in GitHub Desktop.
Save jaredatron/c1bcabc6fc7b8f8c8457 to your computer and use it in GitHub Desktop.
class Tilt::CoffeeScriptTemplate
SYNTAX_ERROR_MESSAGE = /SyntaxError: \[stdin\]:(\d+):(\d+)/
def evaluate_with_better_errors(scope, locals, &block)
evaluate_without_better_errors(scope, locals, &block)
rescue ExecJS::RuntimeError => error
raise error unless error.message.match(SYNTAX_ERROR_MESSAGE)
line, col = $1, $2
message = "SyntaxError: #{@file}:#{line}:#{col}"
message += "\n\n#{source_snippit(@data, line)}"
error.instance_variable_set(:@message, message)
def error.message
@message
end
raise error
end
alias_method_chain :evaluate, :better_errors
private
SNIPPIT_PADDING = 30
def source_snippit(source, line)
line = line.to_i
lines = source.split("\n")
lines = lines.each_with_index.map do |line, index|
"#{index+1}: #{line}"
end
range = [line - SNIPPIT_PADDING / 2, SNIPPIT_PADDING]
if range[1] >= lines.length
range = [lines.length - SNIPPIT_PADDING, SNIPPIT_PADDING]
end
if range[0] < 0
range = [0,SNIPPIT_PADDING]
end
lines.slice(*range).join("\n")
rescue
binding.pry
raise
end
end
@voondo
Copy link

voondo commented Dec 4, 2015

Thanks a lot !

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