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

Klaw stops on error #23

Open
dzek69 opened this issue Jul 13, 2017 · 5 comments
Open

Klaw stops on error #23

dzek69 opened this issue Jul 13, 2017 · 5 comments

Comments

@dzek69
Copy link

dzek69 commented Jul 13, 2017

Hello,

I really appreciate this awesome package, but I cannot configure it to continue on error.

If directory access error occurs (not sure about file errors) then klaw just emits error and stops. end isn't called.

I'd like to continue seeking through directories.

Currently I cannot use klaw to scan whole readable filesystem, because it surely won't have access to some directories.

Any solutions?

@bjorn-nesby
Copy link

You could add a filter function, and "stat" each path using a try...catch.
If it fails, returning false will allow you to skip that troublesome entry.

@ponsoc
Copy link

ponsoc commented Feb 14, 2018

Hi,

I'm running into the same issue and I'm not sure how change the example code to keep it running while ignoring the error. Please advice :-)

@briancampo
Copy link

Depending on what you are doing, this might work. I just wrapped my klaw call with fs-extra's pathExists, and if you want to scan a list of folders just create an outer klaw call that checks only for directories and passes the current folder to the below.

  fs.pathExists(folder, (err, exists) => {
    if (exists) {
      klaw(folder, { depthLimit: depth })
          .pipe(excludeDirFilter)
          .on('data', item => {
            files.push(item.path);
          })
          .on('error', (err, item) => {
            console.log('klaw error: ', item);
            throw new Error(`klawError: ${err}`);
          })
          .on('end', () => {cb(files);}); 
    } else {
      cb({ error: `file/folder does not exist: ${folder}`});
    }
  });

@CTimmerman
Copy link

One solution is to use vanilla Node:

// Vanilla Node walker, hard to check done when using default async implementation.
let walk = dir => {
	fs.readdirSync(dir).forEach(item => {
		let itemPath = path.join(dir, item)
		try {
			var stats = fs.statSync(itemPath)
		} catch (e) {
			console.log(e)
			return
		}
		if (stats.isDirectory()) walk(itemPath)
		else if (stats.isFile()) patch(itemPath)
	})
}
walk(project_folder)
console.log("Done.")

@emptydriptray
Copy link

emptydriptray commented Sep 9, 2019

Did anyone find a solution to this issue? I would prefer to use Klaw over the vanilla Node (as suggested above). Any help is much appreciated !

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

6 participants