diegomura / diegomura/react-pdf

Fix error handling and logging

Open
#3,236 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
16.8k
Forks
1.3k
Avg merge
5h 6m
Merged PRs (30d)
52

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:

```tsx
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](https://react-pdf.org/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"`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.