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

Allow overridable error behavior #46

Closed
mjhenkes opened this issue Sep 6, 2017 · 1 comment
Closed

Allow overridable error behavior #46

mjhenkes opened this issue Sep 6, 2017 · 1 comment

Comments

@mjhenkes
Copy link
Contributor

mjhenkes commented Sep 6, 2017

I have a use case where I would like to be able to fall back to another filesystem if a file is not found in the virtual file system. To enable this functionality and not fork memory-fs, I propose creating a function to throw errors that by default would throw the existing MemoryFileSystemError but would allow enterprising consumers the ability to override the function to provide alternate behavior.
For example:

class MemoryFileSystem {
...
  throwError(fn, args, error) {
    throw new MemoryFileSystemError(...error);
  }
...
  statSync(_path) {
    ...
    else {
      return this.throwError('statSync', arguments, [errors.code.ENOENT, _path, "stat"]);
      // previously
      // throw new MemoryFileSystemError(errors.code.ENOENT, _path, "stat");
  }
...
}

Example Usage:

const MemoryFS = require("memory-fs");
const fs = new MemoryFS();
fs.throwError = function(fn, args, error) {
  // do custom things
};

I'm happy to contribute back but want to see what others think of the idea first.

@mjhenkes
Copy link
Contributor Author

Closing, I think it's more approiate to abstract at a higher level using the exists function to determine which filesystem to choose.

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

1 participant