ExceptionReporter.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/Console.h>
  8. #include <LibJS/Runtime/ConsoleObject.h>
  9. #include <LibJS/Runtime/VM.h>
  10. #include <LibJS/Runtime/Value.h>
  11. #include <LibWeb/Bindings/MainThreadVM.h>
  12. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  13. #include <LibWeb/WebIDL/DOMException.h>
  14. namespace Web::HTML {
  15. void report_exception_to_console(JS::Value value, JS::Realm& realm, ErrorInPromise error_in_promise)
  16. {
  17. auto& console = realm.intrinsics().console_object()->console();
  18. if (value.is_object()) {
  19. auto& object = value.as_object();
  20. auto& vm = object.vm();
  21. auto name = object.get_without_side_effects(vm.names.name).value_or(JS::js_undefined());
  22. auto message = object.get_without_side_effects(vm.names.message).value_or(JS::js_undefined());
  23. if (name.is_accessor() || message.is_accessor()) {
  24. // The result is not going to be useful, let's just print the value. This affects DOMExceptions, for example.
  25. if (is<WebIDL::DOMException>(object)) {
  26. auto const& exception = static_cast<WebIDL::DOMException const&>(object);
  27. dbgln("\033[31;1mUnhandled JavaScript exception{}:\033[0m {}: {}", error_in_promise == ErrorInPromise::Yes ? " (in promise)" : "", exception.name(), exception.message());
  28. } else {
  29. dbgln("\033[31;1mUnhandled JavaScript exception{}:\033[0m {}", error_in_promise == ErrorInPromise::Yes ? " (in promise)" : "", JS::Value(&object));
  30. }
  31. } else {
  32. dbgln("\033[31;1mUnhandled JavaScript exception{}:\033[0m [{}] {}", error_in_promise == ErrorInPromise::Yes ? " (in promise)" : "", name, message);
  33. }
  34. if (is<JS::Error>(object)) {
  35. // FIXME: We should be doing this for DOMException as well
  36. // https://webidl.spec.whatwg.org/#js-DOMException-specialness
  37. // "Additionally, if an implementation gives native Error objects special powers or nonstandard properties (such as a stack property), it should also expose those on DOMException objects."
  38. auto const& error_value = static_cast<JS::Error const&>(object);
  39. dbgln("{}", error_value.stack_string(JS::CompactTraceback::Yes));
  40. console.report_exception(error_value, error_in_promise == ErrorInPromise::Yes);
  41. return;
  42. }
  43. } else {
  44. dbgln("\033[31;1mUnhandled JavaScript exception{}:\033[0m {}", error_in_promise == ErrorInPromise::Yes ? " (in promise)" : "", value);
  45. }
  46. console.report_exception(*JS::Error::create(realm, value.to_string_without_side_effects()), error_in_promise == ErrorInPromise::Yes);
  47. }
  48. // https://html.spec.whatwg.org/#report-the-exception
  49. void report_exception(JS::Completion const& throw_completion, JS::Realm& realm)
  50. {
  51. VERIFY(throw_completion.type() == JS::Completion::Type::Throw);
  52. VERIFY(throw_completion.value().has_value());
  53. report_exception_to_console(*throw_completion.value(), realm, ErrorInPromise::No);
  54. }
  55. }