Skip to content

Commit

Permalink
console preview for Error objects (#1474) (#1474)
Browse files Browse the repository at this point in the history
Summary:
Original Author: edmondc@meta.com
Original Git: 9620a77
Original Reviewed By: huntie
Original Revision: D61243518

X-link: facebook/react-native#46010

Pull Request resolved: #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: lavenzg

Differential Revision: D61574203

fbshipit-source-id: a001e3c4fe51b68820019279bbed7364a5b7403f
  • Loading branch information
dannysu authored and facebook-github-bot committed Aug 27, 2024
1 parent d114d7d commit 9d81af7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions API/hermes/cdp/RemoteObjectConverters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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";
Expand Down

0 comments on commit 9d81af7

Please sign in to comment.