webpack / memory-fs

[DEPRECATED use memfs instead] A simple in-memory filesystem. Holds data in a javascript object.
MIT License
881 stars 88 forks source link

Allow overridable error behavior #46

Closed mjhenkes closed 7 years ago

mjhenkes commented 7 years ago

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 commented 7 years ago

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