@decocms/blocks 7.71.2 → 7.71.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "7.71.2",
3
+ "version": "7.71.3",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -506,6 +506,61 @@ describe("instrumentWorker — OTLP/HTTP error-log channel wiring", () => {
506
506
  expect(payload.resourceLogs[0].scopeLogs[0].logRecords[0].severityText).toBe("info");
507
507
  });
508
508
 
509
+ async function shipConsoleError(...args: unknown[]): Promise<string> {
510
+ const { impl, calls } = makeFetchSpy();
511
+ const handler = {
512
+ fetch: vi.fn(async () => {
513
+ console.error(...args);
514
+ return new Response("ok");
515
+ }),
516
+ };
517
+ const wrapped = instrumentWorker(handler, {
518
+ otlpLogsFetchImpl: impl,
519
+ });
520
+
521
+ const env: TestEnv = {
522
+ DECO_OTEL_LOGS_ENDPOINT: "https://ingest.test/v1/logs",
523
+ } as TestEnv & { DECO_OTEL_LOGS_ENDPOINT: string };
524
+ const ctx = fakeCtx();
525
+ await wrapped.fetch(new Request("https://example.test/"), env, ctx);
526
+ await Promise.all(ctx.waited);
527
+
528
+ expect(calls).toHaveLength(1);
529
+ const payload = JSON.parse(calls[0].body) as {
530
+ resourceLogs: Array<{
531
+ scopeLogs: Array<{ logRecords: Array<{ body: { stringValue: string } }> }>;
532
+ }>;
533
+ };
534
+ return payload.resourceLogs[0].scopeLogs[0].logRecords[0].body.stringValue;
535
+ }
536
+
537
+ it("console.error(err) ships message and stack, not \"{}\"", async () => {
538
+ const body = await shipConsoleError("[VTEX] PLP error:", new TypeError("boom"));
539
+ expect(body).toContain("[VTEX] PLP error: TypeError: boom");
540
+ expect(body).toMatch(/TypeError: boom\n\s+at /); // stack frames shipped
541
+ expect(body).not.toContain("{}");
542
+ });
543
+
544
+ it("keeps enumerable fields of an Error subclass", async () => {
545
+ const err = Object.assign(new Error("circuit open"), { host: "x.vtex.com", status: 503 });
546
+ const body = await shipConsoleError(err);
547
+ expect(body).toContain("Error: circuit open");
548
+ expect(body).toContain('{"host":"x.vtex.com","status":503}');
549
+ });
550
+
551
+ it("expands errors nested in objects and arrays", async () => {
552
+ const body = await shipConsoleError({ error: new Error("nested") }, [new RangeError("in array")]);
553
+ expect(body).toContain('"message":"nested"');
554
+ expect(body).toContain('"name":"RangeError","message":"in array"');
555
+ expect(body).not.toContain('{"error":{}}');
556
+ });
557
+
558
+ it("formats error-like values that are not instanceof Error", async () => {
559
+ const domLike = { name: "AbortError", message: "aborted", stack: "AbortError: aborted\n at x" };
560
+ const body = await shipConsoleError(domLike);
561
+ expect(body).toBe("AbortError: aborted\n at x");
562
+ });
563
+
509
564
  it("otlpLogsEnabled=false disables the channel even when env is set", async () => {
510
565
  const { impl, calls } = makeFetchSpy();
511
566
  const handler = {
package/src/sdk/otel.ts CHANGED
@@ -651,6 +651,42 @@ function configureTracerStack(otlpAdapter: OtlpHttpTracer | null): void {
651
651
  // Boot — wires the loggers/meters once (per worker isolate)
652
652
  // ---------------------------------------------------------------------------
653
653
 
654
+ /**
655
+ * Error's `name`/`message`/`stack` are non-enumerable, so a plain
656
+ * `JSON.stringify(err)` ships `"{}"`. Duck-typed so DOMException and
657
+ * cross-realm errors are covered too.
658
+ */
659
+ function isErrorLike(v: unknown): v is Error {
660
+ return v instanceof Error || (
661
+ typeof v === "object" && v !== null &&
662
+ typeof (v as Error).message === "string" &&
663
+ typeof (v as Error).name === "string" &&
664
+ "stack" in v
665
+ );
666
+ }
667
+
668
+ /** Expands nested errors (`{ error: err }`, `[err]`) instead of `{}`. */
669
+ const errorReplacer = (_key: string, v: unknown) =>
670
+ isErrorLike(v) ? { ...(v as object), name: v.name, message: v.message, stack: v.stack } : v;
671
+
672
+ /** Formats one `console.*` argument as a log-body fragment. */
673
+ function formatConsoleArg(a: unknown): string {
674
+ if (typeof a === "string") return a;
675
+ try {
676
+ if (isErrorLike(a)) {
677
+ const head = a.stack ?? `${a.name}: ${a.message}`;
678
+ // Keep enumerable own fields (e.g. `status`, `host`) the old
679
+ // JSON.stringify path used to ship.
680
+ const { name: _n, message: _m, stack: _s, ...rest } = a as object as Record<string, unknown>;
681
+ const extra = JSON.stringify(rest, errorReplacer);
682
+ return extra === "{}" ? head : `${head} ${extra}`;
683
+ }
684
+ return JSON.stringify(a, errorReplacer);
685
+ } catch {
686
+ return String(a);
687
+ }
688
+ }
689
+
654
690
  /**
655
691
  * Replaces `console.*` with thin shims that route every call through the
656
692
  * framework `logger`, which forwards to the configured adapter (direct-POST
@@ -684,12 +720,7 @@ function patchConsole(state: BootState): void {
684
720
  if (busy) return;
685
721
  busy = true;
686
722
  try {
687
- const msg = args
688
- .map((a) => {
689
- if (typeof a === "string") return a;
690
- try { return JSON.stringify(a); } catch { return String(a); }
691
- })
692
- .join(" ");
723
+ const msg = args.map(formatConsoleArg).join(" ");
693
724
  logger[level](msg);
694
725
  } finally {
695
726
  busy = false;