standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 146 forks source link

Mask stack trace in Electron renderer process #691

Open dsanders11 opened 5 years ago

dsanders11 commented 5 years ago

First off, fantastic project, it was getting to be a real headache to have to use require() in Electron code, and this project provides a much cleaner solution than running everything through webpack.

I have noticed that any errors I get in the DevTools for an Electron window unfortunately don't have an easily visible stacktrace, instead showing everything coming from esm.js.

screen shot 2018-12-16 at 2 38 16 pm

With some more digging, I found that the error does have the stack property which includes the filename the error actually originated in.

Is there anything this project can do out-of-the-box to make these errors nicer? I know it's a more node focused project, but it is very useful for Electron.

Here's a hack I put in place for my own purposes to get the filename, using the error event on window which will fire for any uncaught errors.

window.addEventListener('error', event => {
    if (event.filename.endsWith('esm.js') {
        console.error(event.error.stack);
        return true;
    }
})

This is what it would output for the above:

file:///Users/dsanders/GitHub/my-project/my-file.js:191
            await foo()
            ^

SyntaxError: await is only valid in async function
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:704:10)
jdalton commented 5 years ago

Hi @dsanders11!

Could you create a sample repo for me to check out. We do mask stack traces to remove the esm bits so I'm guessing in this case we missed a spot (though there are places we could do better still).

dsanders11 commented 5 years ago

@jdalton, sure. I just made a fork of standard-things/electron-quick-start and made a branch which added invalid code, can be found here. The error is just like the one in my original screenshot.

jdalton commented 5 years ago

Okay so at a glance, I haven't run it yet, the error is in the renderer process. Is that correct?

dsanders11 commented 5 years ago

@jdalton, yes, correct. It's visible in the DevTools for the renderer process.

jdalton commented 5 years ago

Ah nice! Yes, we currently aren't masking stack traces there, but we should be able to. I'm a talk on esm at the Electron Covalence conference Jan 16 so I'll try to have this added by then 😄

spiritwindy commented 5 years ago

electron:: image node:: image

export default class CanvasNest {

  static version = __VERSION__;
}

@jdalton

spiritwindy commented 5 years ago

image I am learning design patterns,and don't understand why so many call stacks are needed ?

jdalton commented 5 years ago

Hi @spitWind!

The Node module pipeline is already pretty involved. When you throw on top of that parsing and runtime instrumentation there can be more calls in the stack.