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

How to retrieve the file where an error occurred? #159

Open
ericmorand opened this issue Nov 25, 2019 · 3 comments
Open

How to retrieve the file where an error occurred? #159

ericmorand opened this issue Nov 25, 2019 · 3 comments

Comments

@ericmorand
Copy link

ericmorand commented Nov 25, 2019

I'm trying to retrieve the file where an error occurred and all I'm able to catch is a standard JavaScript error with a message but not file.

For example:

index.js

const mdeps = require('module-deps');
const md = mdeps();

md.on('error', (e) => {
    console.warn(e);
});
md.end({ file:'./main.js' });

main.js

require('./partial'); // ./partial exists

require('unknown'); // unknown doesn't

The error thrown is:

Error: Cannot find module 'unknown' from '/home/ericmorand/Projects/module-deps-test'

Except by parsing the error message, which is not a serious option - and even this way I would not be able to get the file name, I can't find a way to retrieve the file where the error happened.

I thought that I would be able to catch the correct file by registering to the file event like this:

const mdeps = require('module-deps');
const md = mdeps();

let currentFile;

md.on('file', (file) => {
    currentFile = file;
});
md.on('error', (e) => {
    console.warn(currentFile); // will print ./partial

    console.warn(e);
});
md.end({ file:'./main.js' });

But the currentFile contains ./partial.js because it is the latest file encountered successfully by module-deps. And it is not where the error happens.

Can someone point me to the solution?

@goto-bus-stop
Copy link
Member

I don't think we have a way to do this right now, but i'd be open to adding info properties like .specifier ('unknown') and .source ('module-deps-test/main.js') or something to resolution errors to make them more useful.

@ericmorand
Copy link
Author

That would be awesome. Is it a big change?

@goto-bus-stop
Copy link
Member

goto-bus-stop commented Dec 13, 2019

no we basically just need some property assignments here in the if (!file) branch:

module-deps/index.js

Lines 180 to 185 in 45ad7b6

self.resolver(id, parent, function onresolve (err, file, pkg, fakePath) {
if (err) return cb(err);
if (!file) return cb(new Error(
'module not found: "' + id + '" from file '
+ parent.filename
));

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

No branches or pull requests

2 participants