Skip to content

fix(api): harden toError against circular-reference payloads #142

@coderabbitai

Description

@coderabbitai

Summary

The toError helper in src/api/client.ts (line 6) calls JSON.stringify(err) without a try/catch guard. This can throw if err contains circular references, masking the original error during normalization.

Location

File: src/api/client.ts, lines 4–7

Suggested Fix

 function toError(err: unknown): Error {
   if (err instanceof Error) return err;
-  return new Error(typeof err === "string" ? err : JSON.stringify(err));
+  if (typeof err === "string") return new Error(err);
+  try {
+    return new Error(JSON.stringify(err));
+  } catch {
+    return new Error(String(err));
+  }
 }

Context

This was flagged during code review of PR #140 (#140) by @mpiton. The logic is preexisting on main; the PR only reformatted the quote style. The fix was deemed out of scope for that PR and deferred here.

See original review comment: #140 (comment)

Requested by: @mpiton

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions