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

feat: catch runtime error #4605

Merged

Conversation

malcolm-kee
Copy link
Contributor

@malcolm-kee malcolm-kee commented Oct 16, 2022

  • This is a bugfix
  • This is a feature
  • This is a code refactor
  • This is a test update
  • This is a docs update
  • This is a metadata update

For Bugs and Features; did you add new tests?

Motivation / Use-Case

  1. Use finite state machine to model the overlay behavior, which can be represented with the diagram below

image

  1. Currently the runtime error is caught but would need additional handling to display the source code that cause the error properly as the filename received in the ErrorEvent will be an URL prefixed with webpack:///. Not sure if we would be able to find the actual code path based on this.

image

Part of #3689.

Breaking Changes

Additional Info

Test results in IE10

compilation error

runtime error

@codecov
Copy link

codecov bot commented Oct 16, 2022

Codecov Report

Base: 92.11% // Head: 91.98% // Decreases project coverage by -0.13% ⚠️

Coverage data is based on head (4bc3bda) compared to base (fe47a0d).
Patch coverage: 62.50% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4605      +/-   ##
==========================================
- Coverage   92.11%   91.98%   -0.14%     
==========================================
  Files          16       16              
  Lines        1661     1659       -2     
  Branches      623      618       -5     
==========================================
- Hits         1530     1526       -4     
- Misses        120      122       +2     
  Partials       11       11              
Impacted Files Coverage Δ
client-src/index.js 81.45% <62.50%> (-0.30%) ⬇️
lib/servers/WebsocketServer.js 89.74% <0.00%> (-5.13%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@malcolm-kee
Copy link
Contributor Author

@alexander-akait the issue that I mentioned in the previous PR about differentiating build error vs runtime error is no longer a concern with this implementation as once any BUILD_ERROR event is emitted, the runtime error will not be displayed.

The next step I need to work on is to find out the source code that throws the runtime error (currently the info I get is the URL prefix with webpack:///), not sure if this is even possible if we do not have control on the source map.

@malcolm-kee
Copy link
Contributor Author

The modeling using finite-state machine is just a random idea that come to me today. Let me know if it's involving too much change and I will revert it.

I'm using it to think through the desired behavior.

@@ -0,0 +1,89 @@
import { createMachine, assign } from "@xstate/fsm";
Copy link
Member

Choose a reason for hiding this comment

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

What is size @xstate/fsm? Because extra size reduce build time

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://bundlejs.com/?q=%40xstate%2Ffsm

It's 3.94kb/1.82kb gzip.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like big... Maybe we can reimplement only necessary things or use more small alternative?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the acceptable size? I can shop around.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just look at @xstate/fsm's implementation, which seems to be quite minimal.

I can think of two options:

  1. use @xstate/fsm as baseline and continue code golf it to make it as small as possible (probably by removing some error check and some convenient API).
  2. pre-minify it before build, but I not sure if this would help the compilation efficiency.

Copy link
Member

Choose a reason for hiding this comment

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

I think the 1. will be better, we can add a new API when we will need it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for no update for more than a month! 😅

I've just created a custom createMachine while maintain enough compatibility so we can use their visualizer.

@malcolm-kee malcolm-kee force-pushed the feat/overlay-catch-runtime-error branch 2 times, most recently from 43e3f51 to 7907c8e Compare November 27, 2022 01:25
Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Looks very good for me

@snitin315 Can you review too, also what do you think about include it in the next (or we can include it in master and get feedback faster)

snitin315
snitin315 previously approved these changes Nov 28, 2022
Copy link
Member

@snitin315 snitin315 left a comment

Choose a reason for hiding this comment

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

Looks great ⭐ . @malcolm-kee can you also update the webpack 4 snapshots?

@alexander-akait I think we can release it in master. next will take time.

@malcolm-kee malcolm-kee marked this pull request as ready for review December 2, 2022 17:05
@malcolm-kee malcolm-kee force-pushed the feat/overlay-catch-runtime-error branch from 62ecee6 to 5298888 Compare December 2, 2022 17:06
@malcolm-kee
Copy link
Contributor Author

I've updated the snapshot for webpack 4.

There are few things that I want to continue work on once this is merged:

  1. Uncaught Promise rejection
  2. Expose additional options to catch custom error for frameworks, e.g. the react-error-overlay has some custom code to catch React component errors
  3. Ability to figure out the actual line of code that throws error. I suppose ability to do this would be dependent on the devtool/source map options, but I think it's still worthwhile to include that capability even it only supports certain use case only.

alexander-akait
alexander-akait previously approved these changes Dec 4, 2022
Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Thank, you will do release tomorrow

@alexander-akait
Copy link
Member

alexander-akait commented Dec 4, 2022

@malcolm-kee looks like four tests for webpack v4 is failed (not sure, why because logic is the same, maybe you are missing something, where we before have workarounds for webpack v4), can you look at them?

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Just to ensure, can you test it using IE11-IE10 (some developer still need it), to ensure we don't use the modern browser API and babel works correctly here, just a manual test, and thank you for your hard work

@malcolm-kee
Copy link
Contributor Author

@alexander-akait is there any workflow/test case that I can refer for testing in IE? It seems like webpack code itself use arrow function, which is not supported in IE.

IE console showing syntax error of arrow function

@malcolm-kee
Copy link
Contributor Author

Also on side note, is it possible for this repo to request free account from Browserstack or Saucelab? I paid for a month subscription to do the testing above 😅

@alexander-akait
Copy link
Member

@malcolm-kee You can send a request to them 😄 I use virtual machines in such case

For testing please use target: ['web', 'es5'], so webpack will generate ES5 code

@malcolm-kee
Copy link
Contributor Author

Based on the fix above, overlay is displayed when there is build error.

However the runtime error is not caught, probably something wrong with window.addEventListener('error', cb) is not behaving as expected in IE. Would need to check further.

@alexander-akait
Copy link
Member

Great, thank you fro this work, let's check if window.addEventListener('error', cb) is not fixable for IE, it is not a problem, because we I think no one use IE for development

@alexander-akait
Copy link
Member

@malcolm-kee friendly ping ⭐

@malcolm-kee
Copy link
Contributor Author

Sorry for the delayed response.
I've find out the reason runtime error catching not working and apply the fix.
The screenshots are attached in PR description.

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

@snitin315 Can you review too, thank you

Copy link
Member

@snitin315 snitin315 left a comment

Choose a reason for hiding this comment

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

Let's release this and gather feedback!

@snitin315
Copy link
Member

@alexander-akait you can merge and release.

@alexander-akait
Copy link
Member

Sorry for big delay, let's merge and release, really very sorry

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.

None yet

3 participants