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

Make errors in getEntry_ be passed to handler (if specified) #42

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
65 changes: 42 additions & 23 deletions src/filer.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,27 +295,33 @@ var Filer = new function() {
* @param {...string} var_args 1-2 paths to lookup and return entries for.
* These can be paths or filesystem: URLs.
*/
var getEntry_ = function(callback, var_args) {
var srcStr = arguments[1];
var destStr = arguments[2];
var getEntry_ = function(callback, opt_errorHandler, var_args) {
var srcStr = arguments[2];
var destStr = arguments[3];

var errorHandler = opt_errorHandler || function(e) { throw e; };

var onError = function(e) {
var error;
if (e.code == FileError.NOT_FOUND_ERR) {
if (destStr) {
throw new Error('"' + srcStr + '" or "' + destStr +
error = new Error('"' + srcStr + '" or "' + destStr +
'" does not exist.');
} else {
throw new Error('"' + srcStr + '" does not exist.');
error = new Error('"' + srcStr + '" does not exist.');
}
} else {
throw new Error('Problem getting Entry for one or more paths.');
error = new Error('Problem getting Entry for one or more paths.');
}
error.code = e.code;
errorHandler(error);
return;
};

// Build a filesystem: URL manually if we need to.
var src = pathToFsURL_(srcStr);

if (arguments.length == 3) {
if (arguments.length == 4) {
var dest = pathToFsURL_(destStr);
self.resolveLocalFileSystemURL(src, function(srcEntry) {
self.resolveLocalFileSystemURL(dest, function(destEntry) {
Expand Down Expand Up @@ -372,7 +378,7 @@ var Filer = new function() {
} else {
srcEntry.copyTo(destDir, newName, opt_successCallback, opt_errorHandler);
}
}, src, dest);
}, opt_errorHandler, src, dest);
}
}

Expand Down Expand Up @@ -479,13 +485,13 @@ var Filer = new function() {

var callback = function(dirEntry) {

cwd_ = dirEntry;
//cwd_ = dirEntry;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?


// Read contents of current working directory. According to spec, need to
// keep calling readEntries() until length of result array is 0. We're
// guarenteed the same entry won't be returned again.
var entries_ = [];
var reader = cwd_.createReader();
var reader = dirEntry.createReader();

var readEntries = function() {
reader.readEntries(function(results) {
Expand All @@ -508,7 +514,7 @@ var Filer = new function() {
if (dirEntryOrPath.isDirectory) { // passed a DirectoryEntry.
callback(dirEntryOrPath);
} else if (isFsURL_(dirEntryOrPath)) { // passed a filesystem URL.
getEntry_(callback, pathToFsURL_(dirEntryOrPath));
getEntry_(callback, opt_errorHandler, pathToFsURL_(dirEntryOrPath));
} else { // Passed a path. Look up DirectoryEntry and proceeed.
// TODO: Find way to use getEntry_(callback, dirEntryOrPath); with cwd_.
cwd_.getDirectory(dirEntryOrPath, {}, callback, opt_errorHandler);
Expand Down Expand Up @@ -542,7 +548,6 @@ var Filer = new function() {
if (folders[0] == '.' || folders[0] == '') {
folders = folders.slice(1);
}

rootDir.getDirectory(folders[0], {create: true, exclusive: exclusive},
function (dirEntry) {
if (dirEntry.isDirectory) { // TODO: check shouldn't be necessary.
Expand All @@ -564,13 +569,11 @@ var Filer = new function() {
}
},
function(e) {
if (e.code == FileError.INVALID_MODIFICATION_ERR) {
e.message = "'" + path + "' already exists";
if (opt_errorHandler) {
opt_errorHandler(e);
} else {
throw e;
}
e.message = e.message || "'" + path + "' already exists";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still want a check for FileError.INVALID_MODIFICATION_ERR. IIRC, the e.message is not informative. Hence the custom one.

if (opt_errorHandler) {
opt_errorHandler(e);
} else {
throw e;
}
}
);
Expand All @@ -597,10 +600,26 @@ var Filer = new function() {
} else {
getEntry_(function(fileEntry) {
fileEntry.file(successCallback, opt_errorHandler);
}, pathToFsURL_(entryOrPath));
}, opt_errorHandler, pathToFsURL_(entryOrPath));
}
};

/**
* Looks up a FileEntry or DirectoryEntry for a given path.
*
* @param {function(...FileEntry|DirectorEntry)} callback A callback to be
* passed the entry/entries that were fetched. The ordering of the
* entries passed to the callback correspond to the same order passed
* to this method.
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opt_errorHandler needs a doc string

Filer.prototype.getEntry = function(path, successCallback, opt_errorHandler) {
if (!fs_) {
throw new Error(FS_INIT_ERROR_MSG);
}

getEntry_(successCallback, opt_errorHandler, pathToFsURL_(path));
};

/**
* Creates an empty file.
*
Expand Down Expand Up @@ -681,7 +700,7 @@ var Filer = new function() {
if (entryOrPath.isFile || entryOrPath.isDirectory) {
removeIt(entryOrPath);
} else {
getEntry_(removeIt, entryOrPath);
getEntry_(removeIt, opt_errorHandler, entryOrPath);
}
};

Expand Down Expand Up @@ -720,7 +739,7 @@ var Filer = new function() {
throw e;
}
}
}, dirEntryOrPath);
}, opt_errorHandler, dirEntryOrPath);
}
};

Expand Down Expand Up @@ -802,7 +821,7 @@ var Filer = new function() {
if (entryOrPath.isFile) {
writeFile_(entryOrPath);
} else if (isFsURL_(entryOrPath)) {
getEntry_(writeFile_, entryOrPath);
getEntry_(writeFile_, opt_errorHandler, entryOrPath);
} else {
cwd_.getFile(entryOrPath, {create: true, exclusive: false}, writeFile_,
opt_errorHandler);
Expand Down