From f1dd2189448305e2ba8d5ac3f483d27039d9af34 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 7 Nov 2016 15:36:40 -0800 Subject: [PATCH] Some extra error handling --- app/backend/sync/index.js | 17 ++++++++++++++--- app/backend/sync/storage.js | 11 +++++++++-- app/ui/components/modals/LoginModal.js | 17 ++++++++++------- app/ui/components/modals/SyncModal.js | 16 ---------------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/app/backend/sync/index.js b/app/backend/sync/index.js index 9f2c583b7..eeee19b98 100644 --- a/app/backend/sync/index.js +++ b/app/backend/sync/index.js @@ -137,7 +137,10 @@ export async function pushActiveDirtyResources (resourceGroupId = null) { // Resolve conflicts for (const serverResource of conflicts) { - const localResource = await store.getResourceByDocId(serverResource.id); + const localResource = await store.getResourceByDocId( + serverResource.id, + serverResource.resourceGroupId + ); // On conflict, choose last edited one const serverIsNewer = serverResource.lastEdited > localResource.lastEdited; @@ -268,7 +271,10 @@ export async function pull (resourceGroupId = null, createMissingResources = tru await db.update(doc, true); // Update local resource - const resource = await store.getResourceByDocId(serverResource.id); + const resource = await store.getResourceByDocId( + serverResource.id, + serverResource.resourceGroupId + ); await store.updateResource(resource, serverResource, {dirty: false}); } catch (e) { logger.warn('Failed to decode updated resource', e, serverResource); @@ -402,6 +408,7 @@ async function _queueChange (event, doc) { // TODO: Remove one of these steps since it does encryption twice // in the case where the resource does not exist yet const resource = await getOrCreateResourceForDoc(doc); + const updatedResource = await store.updateResource(resource, { name: doc.name || 'n/a', lastEdited: timestamp, @@ -620,7 +627,11 @@ async function _getOrCreateAllActiveResources (resourceGroupId = null) { for (const doc of await db.all(type)) { const resource = await store.getResourceByDocId(doc._id); if (!resource) { - activeResourceMap[doc._id] = await _createResourceForDoc(doc); + try { + activeResourceMap[doc._id] = await _createResourceForDoc(doc); + } catch (e) { + logger.error(`Failed to create resource for ${doc._id}`, doc); + } } } } diff --git a/app/backend/sync/storage.js b/app/backend/sync/storage.js index 98dae29c2..661014523 100644 --- a/app/backend/sync/storage.js +++ b/app/backend/sync/storage.js @@ -56,8 +56,15 @@ export async function findResourcesForResourceGroup (resourceGroupId) { return findResources({resourceGroupId}); } -export async function getResourceByDocId (id) { - const rawDocs = await _execDB(TYPE_RESOURCE, 'find', {id}); +export async function getResourceByDocId (id, resourceGroupId = null) { + let query; + if (resourceGroupId) { + query = {id, resourceGroupId}; + } else { + query = {id}; + } + + const rawDocs = await _execDB(TYPE_RESOURCE, 'find', query); return rawDocs.length >= 1 ? rawDocs[0] : null; } diff --git a/app/ui/components/modals/LoginModal.js b/app/ui/components/modals/LoginModal.js index d60667dfa..a92d56658 100644 --- a/app/ui/components/modals/LoginModal.js +++ b/app/ui/components/modals/LoginModal.js @@ -33,8 +33,10 @@ class LoginModal extends Component { await session.login(email, password); // Clear all existing sync data that might be there and enable sync - await sync.resetLocalData(); - sync.doInitialSync(); + process.nextTick(async () => { + await sync.resetLocalData(); + await sync.doInitialSync(); + }); this.setState({step: 2, loading: false}); } catch (e) { @@ -60,8 +62,8 @@ class LoginModal extends Component { const {step, title, message} = this.state; if (step === 1) { return ( - this.modal = m} {...this.props}> -
+ + this.modal = m} {...this.props}> {title || "Login to Your Account"} {message ? ( @@ -96,12 +98,13 @@ class LoginModal extends Component { Signup - -
+
+ ) } else { return ( diff --git a/app/ui/components/modals/SyncModal.js b/app/ui/components/modals/SyncModal.js index 0a8b1ab66..c41db7952 100644 --- a/app/ui/components/modals/SyncModal.js +++ b/app/ui/components/modals/SyncModal.js @@ -165,22 +165,6 @@ class SyncModal extends Component { -
- -
-

- Hi {this.state.firstName}! -

-

- You are currently signed in with - {" "} - {this.state.email} -

-
-
-