From: Pawel Andruszkiewicz Date: Mon, 9 Nov 2015 15:06:38 +0000 (+0100) Subject: [File] Improved error handling. X-Git-Tag: submit/tizen/20151221.111205^2~45^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f2385366be2fdf8f54ec3b8c604523a0aaac633a;p=platform%2Fcore%2Fapi%2Fcordova-plugins.git [File] Improved error handling. [Verification] Code compiles, pass rate: 113/140. Change-Id: Iad793b9050e658b0d3bfc2ddeb404e9678628f0b Signed-off-by: Pawel Andruszkiewicz --- diff --git a/src/file/js/DirectoryEntry.js b/src/file/js/DirectoryEntry.js index 0d442b4..3e32e88 100644 --- a/src/file/js/DirectoryEntry.js +++ b/src/file/js/DirectoryEntry.js @@ -96,8 +96,7 @@ cordova.define('cordova-plugin-file.tizen.DirectoryEntry', function(require, exp ); } catch (err) { console.error('Error - Could not resolve'); - errorCallback && errorCallback( - ConvErrorCode(exception.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(err)); } } else { console.error('Error - create flag is false - new directory would not be created'); @@ -110,8 +109,7 @@ cordova.define('cordova-plugin-file.tizen.DirectoryEntry', function(require, exp tizen.filesystem.resolve(absolute_path, resolveSuccess, resolveError, 'rw'); } catch (err) { console.error('Error - Could not resolve'); - errorCallback && errorCallback( - ConvErrorCode(exception.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(err)); } }; @@ -121,6 +119,13 @@ module.exports = { }, removeRecursively: function(successCallback, errorCallback, args) { var uri = args[0]; + + if (rootsUtils.isRootUri(uri)) { + console.error('It is not allowed to remove root directory.'); + errorCallback && errorCallback(FileError.NO_MODIFICATION_ALLOWED_ERR); + return; + } + // resolve parent var tmp_path = uri[uri.length-1] === '/' ? uri.substring(0, uri.lastIndexOf('/')) : uri; var parent_path = tmp_path.substring(0, tmp_path.lastIndexOf('/')+1); @@ -145,18 +150,16 @@ module.exports = { } } catch (err) { console.error('Error - Could not deleteDirectory'); - errorCallback && errorCallback( - ConvErrorCode(exception.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(err)); } }, function(e) { - console.error('Error' + e.message); - errorCallback && errorCallback(FileError.NOT_FOUND_ERR); + console.error('Error: ' + e.message); + errorCallback && errorCallback(ConvertTizenFileError(e)); }, 'rw' ); } catch (err) { console.error('Error - Could not resolve'); - errorCallback && errorCallback( - ConvErrorCode(exception.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(err)); } }, getFile: function(successCallback, errorCallback, args) { diff --git a/src/file/js/DirectoryReader.js b/src/file/js/DirectoryReader.js index 805e237..3d1d926 100644 --- a/src/file/js/DirectoryReader.js +++ b/src/file/js/DirectoryReader.js @@ -22,7 +22,7 @@ module.exports = { readEntries: function(successCallback, errorCallback, args) { var uri = args[0]; var fail = function(e) { - errorCallback && errorCallback(ConvErrorCode(e.code)); + errorCallback && errorCallback(ConvertTizenFileError(e)); } try { tizen.filesystem.resolve(uri, diff --git a/src/file/js/Entry.js b/src/file/js/Entry.js index 870c411..f9f35c5 100644 --- a/src/file/js/Entry.js +++ b/src/file/js/Entry.js @@ -26,20 +26,18 @@ var resolveParent = function(srcURL, errorCallback, rest){ var parentDir = srcFile.parent; if (!parentDir) { console.error('Error - could not resolve file ' + srcURL); - errorCallback && errorCallback(ConvErrorCode(WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(FileError.ENCODING_ERR); } else { rest(srcFile, parentDir); } }, function (err) { console.error('Error - resolve file ' + srcURL + ' failed'); - errorCallback && errorCallback( - ConvErrorCode(err.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(err)); }, 'r'); } catch (exception) { console.error('Error - resolve ' + srcURL + ' file thrown exception'); - errorCallback && errorCallback(ConvErrorCode( - exception.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(exception)); } }; @@ -64,23 +62,21 @@ var changeFile = function(method, successCallback, errorCallback, args) { successCallback && successCallback(destEntry); }, function (err) { console.error('Error - resolve result entry failed'); - errorCallback && errorCallback(ConvErrorCode(err.code)); + errorCallback && errorCallback(ConvertTizenFileError(err)); } ); } catch (exception) { console.error('Error - resolve result entry thrown exception'); - errorCallback && errorCallback(ConvErrorCode( - exception.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(exception)); } }, function (err) { console.error('Error - ' + method + ' operation failed'); - errorCallback && errorCallback(ConvErrorCode(err.code)); + errorCallback && errorCallback(ConvertTizenFileError(err)); } ); } catch (exception) { console.error('Error - ' + method + ' operation thrown exception'); - errorCallback && errorCallback(ConvErrorCode( - exception.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(exception)); } } ); @@ -93,18 +89,16 @@ module.exports = { var result = { 'size': file.fileSize, 'lastModifiedDate': file.modified }; successCallback && successCallback(result); }, function (err) { - errorCallback && errorCallback(ConvErrorCode( - err.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(err)); }, 'r'); } catch (exception) { console.error('Error - resolve failed'); - errorCallback && errorCallback(ConvErrorCode( - exception.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(exception)); } }, setMetadata: function(successCallback, errorCallback, args) { console.error('setMetadata - Not supported'); - errorCallback && errorCallback(ConvErrorCode(WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(FileError.ENCODING_ERR); }, moveTo: function(successCallback, errorCallback, args) { changeFile('moveTo', successCallback, errorCallback, args); @@ -115,6 +109,12 @@ module.exports = { remove: function(successCallback, errorCallback, args) { var url = args[0]; + if (rootsUtils.isRootUri(url)) { + console.error('It is not allowed to remove root directory.'); + errorCallback && errorCallback(FileError.NO_MODIFICATION_ALLOWED_ERR); + return; + } + resolveParent(url, errorCallback, function(srcFile, parentDir){ var method = srcFile.isFile ? 'deleteFile' : 'deleteDirectory'; @@ -123,8 +123,7 @@ module.exports = { function() {successCallback && successCallback();}, function(err) { console.error('Error - ' + method + ' failed'); - errorCallback && errorCallback( - ConvErrorCode(err.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(err)); }]; if (srcFile.isFile) { //remove recursive flag @@ -134,8 +133,7 @@ module.exports = { parentDir[method].apply(parentDir, args); } catch (exception) { console.error('Error - ' + method + ' thrown exception ' + JSON.stringify(exception)); - errorCallback && errorCallback(ConvErrorCode( - exception.code || WebAPIException.UNKNOWN_ERR)); + errorCallback && errorCallback(ConvertTizenFileError(exception)); } } ); diff --git a/src/file/js/Errors.js b/src/file/js/Errors.js index a0e71b0..89080f5 100644 --- a/src/file/js/Errors.js +++ b/src/file/js/Errors.js @@ -15,18 +15,21 @@ */ /** - * Function converting a tizen error to a cordova error + * Function converting a Tizen error to a cordova error * - * {unsigned short} WebAPIError error code + * {unsigned short} WebAPIError error */ -function ConvErrorCode(err_code) { - switch (err_code) { - case WebAPIException.INVALID_VALUES_ERR: +function ConvertTizenFileError(err) { + switch (err.name) { + case 'InvalidValuesError': return FileError.ENCODING_ERR; - case WebAPIException.NOT_FOUND_ERR: + case 'NotFoundError': return FileError.NOT_FOUND_ERR; + case 'IOError': + return FileError.INVALID_MODIFICATION_ERR; + default: return FileError.ENCODING_ERR; } diff --git a/src/file/js/FileReader.js b/src/file/js/FileReader.js index df12a4f..0d6fe9c 100644 --- a/src/file/js/FileReader.js +++ b/src/file/js/FileReader.js @@ -20,7 +20,7 @@ cordova.define('cordova-plugin-file.tizen.FileReader', function(require, exports function read(operation, url, start, end, successCallback, errorCallback, encoding) { var fail = function(e) { - errorCallback && errorCallback(ConvErrorCode(e.code)); + errorCallback && errorCallback(ConvertTizenFileError(e)); } try { tizen.filesystem.resolve(url, function(file) { diff --git a/src/file/js/FileWriter.js b/src/file/js/FileWriter.js index 91b4222..56e718a 100644 --- a/src/file/js/FileWriter.js +++ b/src/file/js/FileWriter.js @@ -41,29 +41,29 @@ module.exports = { stream.close(); successCallback && successCallback(length); } catch (error) { - errorCallback && errorCallback(ConvErrorCode(error.code)); + errorCallback && errorCallback(ConvertTizenFileError(error)); } } var openStreamError = function (error) { - errorCallback && errorCallback(ConvErrorCode(error.code)); + errorCallback && errorCallback(ConvertTizenFileError(error)); } try { file.openStream('rw', openStreamSuccess, openStreamError); } catch (error) { - errorCallback && errorCallback(ConvErrorCode(error.code)); + errorCallback && errorCallback(ConvertTizenFileError(error)); } } var onError = function (error) { - errorCallback && errorCallback(ConvErrorCode(error.code)); + errorCallback && errorCallback(ConvertTizenFileError(error)); } try { tizen.filesystem.resolve(uri, onSuccess, onError, 'rw'); } catch (error) { - errorCallback && errorCallback(ConvErrorCode(error.code)); + errorCallback && errorCallback(ConvertTizenFileError(error)); } }, @@ -83,7 +83,7 @@ module.exports = { var result = native_.callSync('File_truncate', callArgs); if (native_.isFailure(result)) { - errorCallback && errorCallback(ConvErrorCode(native_.getErrorObject(result).code)); + errorCallback && errorCallback(ConvertTizenFileError(native_.getErrorObject(result))); } else { successCallback && successCallback(length); } diff --git a/src/file/js/resolveLocalFileSystemURI.js b/src/file/js/resolveLocalFileSystemURI.js index 69cc36a..19491fe 100644 --- a/src/file/js/resolveLocalFileSystemURI.js +++ b/src/file/js/resolveLocalFileSystemURI.js @@ -40,7 +40,7 @@ module.exports = { } function onError(error) { - errorCallback && errorCallback(ConvErrorCode(error.code)); + errorCallback && errorCallback(ConvertTizenFileError(error)); } tizen.filesystem.resolve(path, onResolve, onError, 'r'); diff --git a/src/file/js/rootsUtils.js b/src/file/js/rootsUtils.js index a8c81db..4a68bfd 100644 --- a/src/file/js/rootsUtils.js +++ b/src/file/js/rootsUtils.js @@ -111,6 +111,11 @@ var rootsUtils = (function() { return roots[0]; // root filesystem } + function isRootUri(uri) { + var fs = findFilesystem(uri); + return (fs.fullPath === getFullPath(uri)); + } + return { getRoots: getRoots, findFilesystem: findFilesystem, @@ -118,6 +123,7 @@ var rootsUtils = (function() { getFullPath: getFullPath, getNativeUrl: getNativeUrl, stripTrailingSlash: stripTrailingSlash, - createEntry: createEntry + createEntry: createEntry, + isRootUri: isRootUri }; })();