From d114d7d1eaa16c18491a0eb411190e4f750e011f Mon Sep 17 00:00:00 2001 From: Danny Su Date: Mon, 26 Aug 2024 17:18:45 -0700 Subject: [PATCH] Render Error objects in CDT (#1471) (#1471) Summary: Original Author: edmondc@meta.com Original Git: 6b456936748935f206f04824ec6868049abae8f6 Original Reviewed By: hoxyq Original Revision: D60598446 X-link: https://github.com/facebook/react-native/pull/45990 Pull Request resolved: https://github.com/facebook/hermes/pull/1471 Changelog: [General][Added]: support for rendering Error objects in Chrome DevTools console As discussed in [Linkifying and symbolicating JavaScript Error stacks in Fusebox](https://docs.google.com/document/d/1JI_PPzxFRwRNii6pcx-4Cb7g8UTrqnZHCxaoynntIrM/edit) by hoxyq, Error objects in CDT currently displays a big blob of string. In this diff, we send the correct CDT params. Reviewed By: mattbfb Differential Revision: D61574237 fbshipit-source-id: 73829455a50eb2e98a717cf2506edec10c9d758c --- API/hermes/cdp/RemoteObjectConverters.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/API/hermes/cdp/RemoteObjectConverters.cpp b/API/hermes/cdp/RemoteObjectConverters.cpp index afeb206eb1c..355f087b08a 100644 --- a/API/hermes/cdp/RemoteObjectConverters.cpp +++ b/API/hermes/cdp/RemoteObjectConverters.cpp @@ -18,6 +18,13 @@ namespace m = ::facebook::hermes::cdp::message; constexpr size_t kMaxPreviewProperties = 10; +static bool isObjectInstanceOfError( + const jsi::Object &obj, + facebook::jsi::Runtime &runtime) { + return obj.instanceOf( + runtime, runtime.global().getPropertyAsFunction(runtime, "Error")); +} + static m::runtime::PropertyPreview generatePropertyPreview( facebook::jsi::Runtime &runtime, const std::string &name, @@ -309,6 +316,16 @@ m::runtime::RemoteObject m::runtime::makeRemoteObject( if (options.generatePreview) { result.preview = generateArrayPreview(runtime, array); } + } else if (isObjectInstanceOfError(obj, runtime)) { + result.type = "object"; + result.subtype = "error"; + // T198854404 we should report subclasses of Error here, e.g. TypeError + result.className = "Error"; + result.description = + obj.getProperty(runtime, "stack").toString(runtime).utf8(runtime); + if (options.generatePreview) { + result.preview = generateObjectPreview(runtime, obj); + } } else { result.type = "object"; result.description = result.className = "Object";