From 9620a77b59ea2484104335215df8efce8f0cc69c Mon Sep 17 00:00:00 2001 From: Edmond Chui Date: Thu, 15 Aug 2024 19:42:52 -0700 Subject: [PATCH] console preview for Error objects (#1474) Summary: X-link: https://github.com/facebook/react-native/pull/46010 Pull Request resolved: https://github.com/facebook/hermes/pull/1474 Changelog: [General][Added]: support for rendering Error object previews in Chrome DevTools console On web, an array of Error objects have previews. This diff brings the parity to RN DevTools Reviewed By: huntie Differential Revision: D61243518 fbshipit-source-id: d9c6af4b44cef44cb63c4462eee649a8e498a429 --- 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 355f087b08a..e8a1f4753c6 100644 --- a/API/hermes/cdp/RemoteObjectConverters.cpp +++ b/API/hermes/cdp/RemoteObjectConverters.cpp @@ -18,6 +18,18 @@ namespace m = ::facebook::hermes::cdp::message; constexpr size_t kMaxPreviewProperties = 10; +// Parity with V8. 13 Aug, 2024 +// https://source.chromium.org/chromium/chromium/src/+/main:v8/src/inspector/value-mirror.cc;l=191-201;drc=bdc48d1b1312cc40c00282efb1c9c5f41dcdca9a +static std::string abbreviateString(const std::string &str) { + const std::string::size_type kMaxLength = 100; + const std::string kEllipsis = "…"; + if (str.length() <= kMaxLength) { + return str; + } + + return str.substr(0, kMaxLength - 1) + kEllipsis; +} + static bool isObjectInstanceOfError( const jsi::Object &obj, facebook::jsi::Runtime &runtime) { @@ -63,6 +75,11 @@ static m::runtime::PropertyPreview generatePropertyPreview( preview.subtype = "array"; preview.value = "Array(" + std::to_string(obj.getArray(runtime).length(runtime)) + ")"; + } else if (isObjectInstanceOfError(obj, runtime)) { + preview.type = "object"; + preview.subtype = "error"; + preview.value = abbreviateString( + obj.getProperty(runtime, "stack").toString(runtime).utf8(runtime)); } else { preview.type = "object"; preview.value = "Object";