Fix error handling and logging

Open
#3,236 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Domain
backend

Research direction

Start with packages/reconciler/src/reconciler-31.ts at the linked error-handling code, then reproduce the failure using the component snippet and e2e/node-esm/index.js. Confirm that the original rendering error is surfaced rather than replaced, and that it is not emitted through an unformatted console call.

Written by the indexing model from the issue text.

Description

Describe the bug

When you render a PDF in node the actual error gets logged to console.error and a different error gets thrown TypeError: Cannot read properties of null. This has two issues:

  1. When logging errors to Sentry you always get the same error.
  2. When using a formatted logger the console.error pollutes the stdout/stderror with non-formated logs.

When debugging I learned, but rethrowing the error instead of logging it here it worked as expected:
https://github.com/diegomura/react-pdf/blob/4aad0f971a484b75c3ff8de6729de15bc6ffe587/packages/reconciler/src/reconciler-31.ts#L68-L70

I have two suggestions and would be happy to open PRs in case there is interest for them:

  1. instead of console.log/warn/error allow to pass in a logger that by default is console
  2. instead of logging the reconciler errors they are re-thrown

To Reproduce
Steps to reproduce the behavior including code snippet (if applies):

Add this code in a component:

Example in a component:

if ('test' in {}.test2) {
  console.log('Did not fail');
}

and render the PDF in node e.g. https://github.com/diegomura/react-pdf/blob/master/e2e/node-esm/index.js

You can make use of react-pdf REPL to share the snippet

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: MacOS
  • Node
  • React-pdf version "@react-pdf/renderer": "^4.3.0"
Dominant language
TypeScript
Stars
16.9k
Forks
1.3k
Avg merge
11m
Merged PRs (30d)
2

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from diegomura/react-pdf

All issues in diegomura/react-pdf

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.