Skip to content

Handle parser errors without crashing - #334

Open
PNHD wants to merge 2 commits into
extrabacon:masterfrom
PNHD:fix/json-parser-errors
Open

Handle parser errors without crashing#334
PNHD wants to merge 2 commits into
extrabacon:masterfrom
PNHD:fix/json-parser-errors

Conversation

@PNHD

@PNHD PNHD commented Sep 2, 2026

Copy link
Copy Markdown

Fixes #40. Related to #253.

Parser exceptions currently escape from the stdout/stderr stream data callbacks, so invalid parsed output can crash the Node process instead of reaching .end() / PythonShell.run().

This keeps parser failures on the library's existing completion/error surface while preserving EventEmitter semantics:

  • parse stdout/stderr chunks before calling emit(), so exceptions thrown by user message / stderr listeners still propagate normally instead of being misclassified as parser failures
  • store the first stdout/stderr parser exception and route it through the existing PythonShellError completion path
  • prefer a real non-zero Python process error/traceback when both a parser failure and process failure occur, rather than masking the Python error
  • document that parser failures can surface through pythonError, .end(), and PythonShell.run()

Regression coverage includes invalid JSON, custom stderr-parser failures, listener exceptions, and parser-vs-process error precedence.

Relationship to #326

#326 addresses the same JSON parse-failure family by adding a public parseError event/class. This PR keeps the existing completion/error surface instead, also handles custom stderr-parser failures, and keeps EventEmitter listener exceptions outside the parser catch path. I am happy to defer to the maintainers' preferred API direction.

Validation on the current branch:

  • focused parser/listener/error-precedence suite: 5 passing
  • full npm test: 48 passing
  • npx prettier --check index.ts test/test-python-shell.ts README.md
  • git diff --check

@PNHD PNHD mentioned this pull request Sep 2, 2026
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.

Uncaught error when parsing JSON

1 participant