Skip to content

Instantly share code, notes, and snippets.

@chalkygames123
Created February 5, 2026 17:00
Show Gist options
  • Select an option

  • Save chalkygames123/17e39e400782add27346087cf53d8d6c to your computer and use it in GitHub Desktop.

Select an option

Save chalkygames123/17e39e400782add27346087cf53d8d6c to your computer and use it in GitHub Desktop.
Utility to recursively stringify and error object with causes
export const stringifyErrorChain = (error: unknown, level = 0) => {
let current: unknown = error;
const lines: string[] = [];
while (current instanceof Error) {
const indent = ' '.repeat(level);
lines.push(`${indent}${current.name}: ${current.message}`);
current = current.cause;
level += 1;
}
return lines.join('\n');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment