Browse Source

resolves #1398 fix NPE if error occurs retrieving the parents from the server

AndyScherzinger 7 years ago
parent
commit
a89a46a417

+ 18 - 2
src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -44,6 +44,7 @@ import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.lib.resources.status.CapabilityBooleanType;
 import com.owncloud.android.lib.resources.status.OCCapability;
+import com.owncloud.android.operations.RemoteOperationFailedException;
 import com.owncloud.android.utils.FileStorageUtils;
 import com.owncloud.android.utils.MimeType;
 import com.owncloud.android.utils.MimeTypeUtil;
@@ -250,6 +251,14 @@ public class FileDataStorageManager {
         return overriden;
     }
 
+    /**
+     * traverses a files parent tree to be able to store a file with its parents.
+     * Throws a RemoteOperationFailedException in case the parent can't be retrieved.
+     *
+     * @param file the file
+     * @param context the app context
+     * @return the parent file
+     */
     public OCFile saveFileWithParent(OCFile file, Context context) {
         if (file.getParentId() == 0 && !file.getRemotePath().equals("/")) {
             String remotePath = file.getRemotePath();
@@ -267,8 +276,15 @@ public class FileDataStorageManager {
 
                     returnFile = saveFileWithParent(remoteFolder, context);
                 } else {
-                    returnFile = null;
-                    Log_OC.e(TAG, "Error during saving file with parents: " + file.getRemotePath());
+                    Exception exception = result.getException();
+                    String message = "Error during saving file with parents: " + file.getRemotePath() + " / "
+                            + result.getLogMessage();
+
+                    if (exception != null) {
+                        throw new RemoteOperationFailedException(message, exception);
+                    } else {
+                        throw new RemoteOperationFailedException(message);
+                    }
                 }
             } else {
                 returnFile = saveFileWithParent(parentFile, context);

+ 32 - 0
src/main/java/com/owncloud/android/operations/RemoteOperationFailedException.java

@@ -0,0 +1,32 @@
+package com.owncloud.android.operations;
+
+/**
+ * RuntimeException for throwing errors of remote operation calls.
+ */
+public class RemoteOperationFailedException extends RuntimeException {
+    /**
+     * Constructs a new runtime exception with the specified detail message and
+     * cause.
+     *
+     * @param message the detail message (which is saved for later retrieval
+     *                by the {@link #getMessage()} method).
+     * @param cause   the cause (which is saved for later retrieval by the
+     *                {@link #getCause()} method).  (A <code>null</code> value
+     *                is permitted, and indicates that the cause is nonexistent
+     *                or unknown.)
+     */
+    public RemoteOperationFailedException(String message, Throwable cause) {
+        super(message + " / " + cause.getMessage(), cause);
+    }
+
+    /**
+     * Constructs a new runtime exception with the specified detail message and
+     * cause.
+     *
+     * @param message the detail message (which is saved for later retrieval
+     *                by the {@link #getMessage()} method).
+     */
+    public RemoteOperationFailedException(String message) {
+        super(message);
+    }
+}

+ 14 - 8
src/main/java/com/owncloud/android/ui/adapter/FileListListAdapter.java

@@ -59,6 +59,7 @@ import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
 import com.owncloud.android.lib.resources.files.RemoteFile;
 import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.lib.resources.shares.ShareType;
+import com.owncloud.android.operations.RemoteOperationFailedException;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
 import com.owncloud.android.ui.activity.ComponentsGetter;
 import com.owncloud.android.ui.activity.FileActivity;
@@ -587,17 +588,22 @@ public class FileListListAdapter extends BaseAdapter {
         for (int i = 0; i < objects.size(); i++) {
             OCFile ocFile = FileStorageUtils.fillOCFile((RemoteFile) objects.get(i));
             searchForLocalFileInDefaultPath(ocFile);
-            ocFile = mStorageManager.saveFileWithParent(ocFile, mContext);
 
-            if (!onlyImages || MimeTypeUtil.isImage(ocFile)) {
-                mFiles.add(ocFile);
-            }
+            try {
+                ocFile = mStorageManager.saveFileWithParent(ocFile, mContext);
+
+                if (!onlyImages || MimeTypeUtil.isImage(ocFile)) {
+                    mFiles.add(ocFile);
+                }
 
-            ContentValues cv = new ContentValues();
-            cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, type.toString());
-            cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
+                ContentValues cv = new ContentValues();
+                cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, type.toString());
+                cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
 
-            contentValues.add(cv);
+                contentValues.add(cv);
+            } catch (RemoteOperationFailedException e) {
+                Log_OC.e(TAG, "Error saving file with parent" + e.getMessage(),e);
+            }
         }
 
         mStorageManager.saveVirtuals(type, contentValues);