Browser: Show a "source location hint" for syntax errors :^)

This commit is contained in:
Linus Groh 2020-05-26 13:13:12 +01:00 committed by Andreas Kling
parent 015d65bc6f
commit 0f2b3cd280
Notes: sideshowbarker 2024-07-19 06:06:43 +09:00

View File

@ -79,15 +79,18 @@ ConsoleWidget::ConsoleWidget()
auto parser = JS::Parser(JS::Lexer(js_source));
auto program = parser.parse_program();
StringBuilder output_html;
if (parser.has_errors()) {
auto error = parser.errors()[0];
auto hint = error.source_location_hint(js_source);
if (!hint.is_empty())
output_html.append(String::format("<pre>%s</pre>", hint.characters()));
m_interpreter->throw_exception<JS::SyntaxError>(error.to_string());
} else {
m_interpreter->run(*program);
}
if (m_interpreter->exception()) {
StringBuilder output_html;
output_html.append("Uncaught exception: ");
output_html.append(JS::MarkupGenerator::html_from_value(m_interpreter->exception()->value()));
print_html(output_html.string_view());