[File] Improved error handling.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 9 Nov 2015 15:06:38 +0000 (16:06 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 9 Nov 2015 15:06:38 +0000 (16:06 +0100)
[Verification] Code compiles, pass rate: 113/140.

Change-Id: Iad793b9050e658b0d3bfc2ddeb404e9678628f0b
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/file/js/DirectoryEntry.js
src/file/js/DirectoryReader.js
src/file/js/Entry.js
src/file/js/Errors.js
src/file/js/FileReader.js
src/file/js/FileWriter.js
src/file/js/resolveLocalFileSystemURI.js
src/file/js/rootsUtils.js

index 0d442b4..3e32e88 100644 (file)
@@ -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) {
index 805e237..3d1d926 100644 (file)
@@ -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,
index 870c411..f9f35c5 100644 (file)
@@ -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));
           }
         }
     );
index a0e71b0..89080f5 100644 (file)
  */
 
 /**
- * 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;
   }
index df12a4f..0d6fe9c 100644 (file)
@@ -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) {
index 91b4222..56e718a 100644 (file)
@@ -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);
     }
index 69cc36a..19491fe 100644 (file)
@@ -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');
index a8c81db..4a68bfd 100644 (file)
@@ -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
   };
 })();