Skip to content

fix(log): keep Error details in logger fields - #583

Open
RaphaelFakhri wants to merge 1 commit into
voidzero-dev:mainfrom
RaphaelFakhri:fix/log-error-fields
Open

RaphaelFakhri wants to merge 1 commit into
voidzero-dev:mainfrom
RaphaelFakhri:fix/log-error-fields

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Fixes #577

Summary

logger.error('save failed', { error }) logged "error":{}. name, message, stack and cause are non-enumerable on Error, so JSON.stringify drops them.

format() in packages/void/src/runtime/log.ts now passes a replacer to JSON.stringify. The replacer writes each Error as its own enumerable fields plus name, message, stack and cause. This covers nested errors and cause chains. The { msg, ...fields } shape is unchanged. An error that appears a second time (for example error.cause = error) is written as { name, message }, so serialization terminates and the other fields are kept.

An error with its own toJSON still uses it, because JSON.stringify calls toJSON before the replacer.

Tests

New cases in packages/void/test/unit/runtime-log.test.ts:

  • Error fields keep name, message, stack and cause.
  • Error subclasses keep their own enumerable fields (status).
  • A cyclic cause does not drop the other fields.

vp test run packages/void/test/unit/runtime-log.test.ts: 3 failed, 12 passed without the fix; 15 passed with it. vp lint and vp fmt --check are clean on the changed files.

Docs

docs/reference/api/resources.md notes that you can pass a caught Error in fields.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

void/log serializes Error fields as {}, losing message and stack

1 participant