Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preact 10 more difficult to debug uncaught errors than Preact 8. #2323

Open
PepsRyuu opened this issue Feb 9, 2020 · 4 comments
Open

Preact 10 more difficult to debug uncaught errors than Preact 8. #2323

PepsRyuu opened this issue Feb 9, 2020 · 4 comments

Comments

@PepsRyuu
Copy link

PepsRyuu commented Feb 9, 2020

Recently giving Preact 10 a try after using Preact 8 for a very long time, but having massive difficulties working with it cause of the inability to properly debug with it.

Let's say I had something like the following:

class MyComponent extends Component {
    componentDidMount () {
        a + b; // throws an uncaught error
    }

    render () {
        return <div>Hello World</div>
    }
}

In Preact 8, this will happen:

preact8

In Preact 10, this will happen:

preact10

In Preact 8, it's super easy to see what went wrong, and to inspect variables and try out different snippets of code to try and fix the issue. In Preact 10 because of the try/catch for supporting componentDidCatch, I can't do that anymore, making it really frustrating to identify the issue and solve it, especially when using eval source maps where you can't often can't use the stack trace to click on the relevant line.

Is there any way to resolve this? Can Preact's code be re-organised so that there's no try/catch if there's no componentDidCatch in the component tree? This issue can make fixing code fairly time consuming so would appreciate any thoughts you have.

Thanks! 🙂

@developit
Copy link
Member

We need some form of exception catching in core in order to support componentDidCatch, but perhaps there's a way to make it conditional on the existence of a componentDidCatch somewhere in the tree. That wouldn't solve all cases, but it would at least fix the case where nothing is catching and we're just rethrowing the unmodified error.

FWIW as a short-term fix, if you check the "Pause on caught exceptions" box, the debugger will pause in the same place it used to in 8.x (and then again where you noted it pausing in 10, though you can blackbox preact.js to disable that).

@wnayes
Copy link

wnayes commented Feb 14, 2020

React avoids caught exception issues in debug with its invokeGuardedCallbackImpl. I can understand that including this sort of mechanism might bloat the package. (Well, it could be included for non-minified debug builds only, but I don't know if most Preact users run this sort of build in practice.)

I would be interested in either a built-in solution, or some way that I could provide my own sort of invokeGuardedCallbackImpl implementation for Preact to use at runtime.

options.invokeGuardedCallback = function(callback) {
    /* does something like invokeGuardedCallbackImpl to invoke callback */
}

The existence check on componentDidCatch would also be a good solution for my use case.

@developit
Copy link
Member

Interesting - we have a hook called options. _catchError - maybe this could be a replacement for that hook?

@Megabyteceer
Copy link

Maybe for dev mode you can use setTimeout at the beginning and clearTimeout at the end of critical section. So if timeout is cleared - no error has occurred. If timeout is triggered - you invoke componentDidCatch.
Debugging is really much harder in v10. I just removed all try catch out of preact.js and debugging become beautiful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants