ExceptionReporter.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibJS/Runtime/VM.h>
  8. #include <LibJS/Runtime/Value.h>
  9. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  10. namespace Web::HTML {
  11. // https://html.spec.whatwg.org/#report-the-exception
  12. void report_exception(JS::Completion const& throw_completion)
  13. {
  14. // FIXME: This is just old code, and does not strictly follow the spec of report an exception.
  15. // FIXME: We should probably also report these exceptions to the JS console.
  16. VERIFY(throw_completion.type() == JS::Completion::Type::Throw);
  17. VERIFY(throw_completion.value().has_value());
  18. auto thrown_value = *throw_completion.value();
  19. if (thrown_value.is_object()) {
  20. auto& object = thrown_value.as_object();
  21. auto& vm = object.vm();
  22. auto name = object.get_without_side_effects(vm.names.name).value_or(JS::js_undefined());
  23. auto message = object.get_without_side_effects(vm.names.message).value_or(JS::js_undefined());
  24. if (name.is_accessor() || message.is_accessor()) {
  25. // The result is not going to be useful, let's just print the value. This affects DOMExceptions, for example.
  26. dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", thrown_value);
  27. } else {
  28. dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m [{}] {}", name, message);
  29. }
  30. if (is<JS::Error>(object)) {
  31. auto const& error_value = static_cast<JS::Error const&>(object);
  32. for (auto const& traceback_frame : error_value.traceback()) {
  33. auto const& function_name = traceback_frame.function_name;
  34. auto const& source_range = traceback_frame.source_range;
  35. dbgln(" {} at {}:{}:{}", function_name, source_range.filename, source_range.start.line, source_range.start.column);
  36. }
  37. }
  38. } else {
  39. dbgln("\033[31;1mUnhandled JavaScript exception:\033[0m {}", thrown_value);
  40. }
  41. }
  42. }