镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

"Just My Code" for stack traces? #20505

Description

@benjamingr

Throwing a raw idea out there:

I just saw this question where a user was confused about a stack trace they got when using puppeteer:

Unhandled Rejection at: Promise Promise {
  <rejected> Error: Navigation Timeout Exceeded: 30000ms exceeded
    at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
    at <anonymous> } reason: Error: Navigation Timeout Exceeded: 30000ms exceede
d
    at Promise.then (C:\...\pupet test\node_modules\pupp
eteer\lib\NavigatorWatcher.js:71:21)
    at <anonymous>

The point is, the first few stack frames are irrelevant to the user. If the first stack frame showed the part in their code that caused the error it would have been a lot more useful.

I'm wondering if we can improve the debugging experience of users by giving them "Just My Code"

Just My Code

Node could expose a "Just My Code" exception mode similar to the way stacks work in the Chrome devtools. Running Node with the flag would hide any frames from node_modules folder.

Thoughts?

Activity

  1. added
    discussIssues opened for discussion and feedback.
    promisesIssues and PRs related to ECMAScript promises.
    errorsIssues and PRs related to JavaScript errors originating in Node.js core.
    on May 3, 2018
  2. apapirovski commented on May 3, 2018

    @apapirovski
    Contributor

    Seems pretty well suited to the user-land, no? Just use a custom Error.prepareStackTrace that filters out Node.js core modules & node_modules code. There are some edge cases but I don't think that's a good reason to make it be a part of the core.

    (Can also do all kinds of other neat things with the API https://github.com/v8/v8/wiki/Stack-Trace-API)

  3. bnoordhuis commented on May 8, 2018

    @bnoordhuis
    Member

    One argument in favor is that only core can disambiguate stack frames with 100% certainty.

    vm.runInThisContext('throw new TypeError()', {filename:'vm.js'}) throws an exception that looks like it comes from the vm module, but doesn't, and there's really no way for a user land hook to tell.

    That said, I don't necessarily think hiding stack frames is a good idea. I can quickly see that turning into a bigger hindrance than a help, with people filing bug reports with unhelpful stack traces. Getting a good bug report is already more the exception than the norm, in this day and age.

  4. techsin commented on Oct 5, 2018

    @techsin

    maybe a plugin that make colors of files in node_modules gray and less contrast so you can see what file are yours

  5. jasnell commented on Oct 5, 2018

    @jasnell
    Member

    If it can be done reasonably well I think this would be excellent to have but I definitely share @bnoordhuis' concern around it. It wouldn't hurt to have some non-committal experimentation around it.

  6. techsin commented on Oct 5, 2018

    @techsin

    so what im saying wont hide it, but only visually color it differently in terminal.

  7. techsin commented on Oct 5, 2018

    @techsin

    i created a simple script that accomplishes what i was thinking....
    I simply put it in main app.js file

    const colors = require('colors');
    Error.prepareStackTrace = (err, arr) => {
       let lines = err.stack.split('\n');
       lines = lines.map(x => x.includes('node_modules') ? colors.grey(x) : x);
       lines = lines.join('\n');
       err.stack = lines;
    };
    

    then in one of my routes of app

    throw new Error('test');

    result ....

    capture

    https://github.com/v8/v8/wiki/Stack-Trace-API

  8. AyushG3112 commented on Oct 6, 2018

    @AyushG3112
    Contributor

    Problem with just different Coloring is when you use SDKs which transfer the flow of control a lot.

    I use the aws-sdk a lot and the stack traces there are not useful because it is filled with the stack of the internal function calls, hiding frames from node_modules option will help a lot in this case.

  9. techsin commented on Oct 6, 2018

    @techsin

    didn't explain how coloring won't help sdk traces which live in node_modules...

  10. joyeecheung commented on Nov 27, 2018

    @joyeecheung
    Member

    Just realize that we have a default Error.stackTraceLimit = 10, so unless users set this to a higher value, many of our internal stack frames are probably not going to be visible to them anyway if their code throws in a sufficiently deep call stack.

    Another idea would be to exclude all the stack frames up to the first frame of user code (on master it's usually Module._compile and 8 frames below)

  11. BridgeAR commented on Jan 4, 2020

    @BridgeAR
    Member

    This is partially fixed since a while. Using console.log or util.inspect on errors marks Node.js stack frames grey and adds an underscore to node_modules names. We could go ahead and add a color to node_modules frames but not everyone wanted that. Should this stay open or shall we consider it as solved?

  12. jasnell commented on Jun 19, 2020

    @jasnell
    Member

    I'd say let's consider it solved. If someone wishes to make further enhancements, a PR with a concrete proposal would be ideal. Closing

  13. jfoclpf commented on Oct 6, 2021

    @jfoclpf

    @joyeecheung Error.stackTraceLimit doesn't solve the issue, it just changes the number of lines it prints.

    For example in the following stack, only two lines (related to file prepareServer) were related to files I developed (followed by <===). The others for me are noise

    Error
        at buildAdministrationsObject (/home/joao/dev/geoptapi/prepareServer.js:350:11) <===
        at /home/joao/dev/geoptapi/node_modules/async/dist/async.js:3638:28
        at replenish (/home/joao/dev/geoptapi/node_modules/async/dist/async.js:443:21)
        at iterateeCallback (/home/joao/dev/geoptapi/node_modules/async/dist/async.js:427:21)
        at /home/joao/dev/geoptapi/node_modules/async/dist/async.js:324:20
        at /home/joao/dev/geoptapi/node_modules/async/dist/async.js:3643:17
        at /home/joao/dev/geoptapi/prepareServer.js:158:7 <===
        at wrapper (/home/joao/dev/geoptapi/node_modules/async/dist/async.js:271:20)
        at iterateeCallback (/home/joao/dev/geoptapi/node_modules/async/dist/async.js:424:28)
        at /home/joao/dev/geoptapi/node_modules/async/dist/async.js:324:20
    

    If I change Error.stackTraceLimit = 6 for example I will miss my second line.

    I just want to filter out lines including node_modules

  14. jfoclpf commented on Oct 6, 2021

    @jfoclpf

    Came up with this solution that strips out any line with /node_modules/ or \node_modules\

    const stripedStack = (new Error().stack).replace(/^.*[\\/]node_modules[\\/].*$/gm, '').replace(/\n+/g, '\n')
    console.error(stripedStack)

    Now as I wanted :)

    Error
        at readShapefile (/home/joao/dev/geoptapi/prepareServer.js:106:16)
        at /home/joao/dev/geoptapi/prepareServer.js:100:7
    

    what do you think @jasnell ?

  15. taljacob2 commented on Sep 22, 2023

    @taljacob2

    I edited @techsin comment with the following:

    const colors = require('colors');
    
    Error.prepareStackTrace = (err) => {
        let lines = err.stack.split('\n');
        lines = lines.map((x) => {
            return x.includes('node_modules') ? colors.grey(x) : x;
        });
        lines = lines.map((x) => {
            return x.includes('(node:') ? colors.grey(x) : x;
        });
        lines = lines.join('\n');
        return lines;
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussIssues opened for discussion and feedback.errorsIssues and PRs related to JavaScript errors originating in Node.js core.promisesIssues and PRs related to ECMAScript promises.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions