Skip to content

fix: add context to corrupted entity state errors#309

Open
YunchuWang wants to merge 1 commit into
microsoft:mainfrom
YunchuWang:copilot-finds/bug/entity-getstate-error-handling
Open

fix: add context to corrupted entity state errors#309
YunchuWang wants to merge 1 commit into
microsoft:mainfrom
YunchuWang:copilot-finds/bug/entity-getstate-error-handling

Conversation

@YunchuWang

Copy link
Copy Markdown
Member

Summary

  • Wrap entity state JSON parsing failures with entity-specific context.
  • Preserve the original parsing error as the cause.
  • Add regression coverage for corrupted serialized state.

Fixes #208

Testing

  • npm test -w @microsoft/durabletask-js -- --runTestsByPath test/entity-executor.spec.ts
  • npm run build:core

… for corrupted state

StateShim.getState() calls JSON.parse without error handling, producing
a raw SyntaxError when entity state from the sidecar is corrupted. This
is inconsistent with setState(), which wraps JSON.stringify errors with
a descriptive message and preserves the cause.

The fix adds a try-catch to getState() that wraps JSON.parse errors with
context: 'Entity state contains invalid JSON: <details>', matching the
error handling pattern used in setState().

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings July 17, 2026 16:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves diagnostics for corrupted entity state returned from the sidecar by wrapping JSON.parse failures in StateShim.getState() with a descriptive error message while preserving the original parsing error as the cause. It also adds regression tests to ensure entity execution records failures cleanly when initial serialized state is invalid JSON.

Changes:

  • Wrap StateShim.getState() JSON parsing errors with a contextual message and preserve the original error via ErrorOptions.cause.
  • Add regression coverage in entity-executor.spec.ts for invalid initial entity state JSON, including cause preservation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/durabletask-js/src/worker/entity-executor.ts Wraps corrupted entity state JSON parse errors with a clearer message and preserves the original error as cause.
packages/durabletask-js/test/entity-executor.spec.ts Adds tests asserting failure recording and cause preservation for corrupted serialized entity state.

Comment on lines +622 to +624
it("should rollback to initial state when getState fails mid-batch", async () => {
// First operation succeeds with valid state, second operation gets corrupted state
// This tests that rollback works correctly for getState failures too
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[copilot-finds] Bug: Entity getState() produces unhelpful SyntaxError for corrupted state

3 participants