Skip to content

more useable fs/promises backtraces on error #50160

Closed
@axkibe

Description

@axkibe

What is the problem this feature will solve?

Take following minimal test case:

import fs from 'node:fs/promises';
async function b( ) { return await fs.readFile( 'inexistent' ); }
async function a( ) { return await b( ); }
await a( );

the output will be:

      internalBinding('errors').triggerUncaughtException(
                                ^

[Error: ENOENT: no such file or directory, open 'inexistent'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'inexistent'
}

Node.js v20.8.0

so no hint where.

What is the feature you are proposing to solve the problem?

I used to wrap all fs call to something like this:

import fs from 'node:fs/promises';
async function myread( filename )
{
    try { return await fs.readFile( filename ); }
    catch( e ) { throw new Error( 'cannot read "' + filename + '"' ); }
}
async function b( ) { return await myread( 'inexistent' ); }
async function a( ) { return await b( ); }
await a( );

So the error stack becomes:

file:///home/axel/xtest2/x2.mjs:6
	catch( e ) { throw new Error( 'cannot read "' + filename + '"' ); }
	                   ^

Error: cannot read "inexistent"
    at myread (file:///home/axel/xtest2/x2.mjs:6:21)
    at async b (file:///home/axel/xtest2/x2.mjs:8:30)
    at async a (file:///home/axel/xtest2/x2.mjs:9:30)
    at async file:///home/axel/xtest2/x2.mjs:10:1

Suggestion: IMO it would be beneficial for node to make these more userfriendly/useful backtraces by default without having to wrap all fs calls by the user?

What alternatives have you considered?

explained above, wrap the fs calls with try/catch blocks all myself, it's very possible, likely someone wrote a npm package anyway, likely several, but IMO this is one of the things the core should come with making the entry easier for new people.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions