fix(Formatter): include Error message in formatJson - #8150
Open
Tyagiquamar wants to merge 1 commit into
Open
Tyagiquamar wants to merge 1 commit into
Tyagiquamar wants to merge 1 commit into
Conversation
Fixes Effect-TS#8144 `Error#message` is a non-enumerable own property, so the JSON.stringify replacer inside formatJson rendered plain Errors as `{}`, dropping the message from Logger.formatJson / Logger.consoleJson output. Return safeToString(error) for values that are instanceof Error and do not define a toJSON property, so tagged errors keep their structured serialization.
🦋 Changeset detectedLatest commit: 1f449e8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 30 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
Bundle Size AnalysisGenerated from PR build output; treat the content below as untrusted.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8144
What
Formatter.formatJsonnow stringifies plainErrorvalues usingtoString()instead of serializing them to{}. This makesLogger.formatJson/Logger.consoleJsoninclude the error message when a plain (non-tagged)Erroris logged, e.g. viaEffect.tapError(Effect.logError):Before:
{"message":{},"level":"ERROR",...}After:
{"message":"Error: This message is currently not parsed","level":"ERROR",...}Why
Error#messageis a non-enumerable own property, soJSON.stringify(whichformatJsonuses internally) renders plain errors as{}, dropping the message.Data.TaggedErrorand friends are unaffected because they definetoJSON(their serialized fields flow through that path). As noted in the issue,Effect.tapCausedoes render causes nicely, but any code path that hands a plainErrorto a JSON logger loses the message today.How
The
formatJsonreplacer now returnssafeToString(error)for values that areinstanceof Errorand do not define atoJSONproperty (prototype chain included), so tagged errors keep their structured serialization. This reuses the existingsafeToStringhelper, so an error whosetoStringthrows renders as"[toString threw]"like elsewhere in the formatter.Tests
packages/effect/test/Formatter.test.ts:formatJson(new Error("boom"))is"Error: boom", also nested inside an object.packages/effect/test/Logger.test.ts: end-to-endEffect.tapError(Effect.logError)throughLogger.formatJsonproducesmessage: "Error: boom"at levelERROR.Both fail before the change and pass after. Verified with
pnpm test --run packages/effect/test/Formatter.test.ts,pnpm test --run packages/effect/test/Logger.test.ts,pnpm lint-fix, andpnpm check.