From 8d32fa253a4af07b694ebcd986356dcce47ad399 Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 13 Feb 2023 15:59:41 +0100 Subject: [PATCH] Bug fix --- src/db/change-handler.ts | 12 +++-- src/db/gpx.ts | 98 ++++++++++++++++++++++++---------------- 2 files changed, 66 insertions(+), 44 deletions(-) diff --git a/src/db/change-handler.ts b/src/db/change-handler.ts index 31f79a4..72b1c05 100644 --- a/src/db/change-handler.ts +++ b/src/db/change-handler.ts @@ -26,10 +26,14 @@ const methods = { const sendUpdate = async (params: any) => { if (params) { console.log({ caller: 'ChangeHandler / sendUpdate', params }); - const { method, _dispatchId, id, ...otherParams } = params; - const returnValue = await methods[method](params); - if (returnValue) { - returnAgain(_dispatchId, returnValue); + try { + const { method, _dispatchId, id, ...otherParams } = params; + const returnValue = await methods[method](params); + if (returnValue) { + returnAgain(_dispatchId, returnValue); + } + } catch (error) { + console.error({ caller: 'ChangeHandler / sendUpdate', params, error }); } } }; diff --git a/src/db/gpx.ts b/src/db/gpx.ts index babaad0..ebc7511 100644 --- a/src/db/gpx.ts +++ b/src/db/gpx.ts @@ -420,53 +420,71 @@ export const appendTrk = async (params: any) => { export const putGpx = async (params: any) => { let { id, gpx } = params; - if (id === 'new') { - const date = !!gpx.metadata.time ? new Date(gpx.metadata.time) : new Date(); - id = getUri('gpx', { - gpx: intToGpxId(date.valueOf()), - }); - } - - const previousShared = (await get(`${id}/4extensions`)).doc?.shared; - - console.log({ caller: 'putGpx', params, id, gpx, previousShared }); - const extensions = gpx?.extensions; - - gpx.extensions = undefined; - gpx.wpt = undefined; - gpx.trk = undefined; - gpx.rte = undefined; - - await put(id, 'gpx', (doc) => gpx, gpx); - if (extensions !== undefined) { - try { - await put( - `${id}/4extensions`, - 'extensions', - (doc) => extensions, - extensions - ); - } catch (error) { - console.error({ caller: 'putGpx / extensions', error }); + try { + if (id === 'new') { + const date = !!gpx.metadata.time + ? new Date(gpx.metadata.time) + : new Date(); + id = getUri('gpx', { + gpx: intToGpxId(date.valueOf()), + }); } - } - if (previousShared !== extensions?.shared) { - const shared = extensions?.shared.split(/\s*,\s*/); - const family = (await getFamily(id, { include_docs: true })).rows.map( - (row: any) => { - return { ...row.doc, shared }; + let previousShared; + try { + previousShared = (await get(`${id}/4extensions`)).doc?.shared; + } catch (error) {} + + console.log({ caller: 'putGpx', params, id, gpx, previousShared }); + const extensions = gpx?.extensions; + + gpx.extensions = undefined; + gpx.wpt = undefined; + gpx.trk = undefined; + gpx.rte = undefined; + + await put(id, 'gpx', (doc) => gpx, gpx); + if (extensions !== undefined) { + try { + await put( + `${id}/4extensions`, + 'extensions', + (doc) => extensions, + extensions + ); + } catch (error) { + console.error({ + caller: 'putGpx / extensions', + error, + }); } - ); - console.log({ - caller: 'putGpx / updateShared', + } + + if (previousShared !== extensions?.shared) { + const shared = extensions?.shared.split(/\s*,\s*/); + const family = (await getFamily(id, { include_docs: true })).rows.map( + (row: any) => { + return { ...row.doc, shared }; + } + ); + console.log({ + caller: 'putGpx / updateShared', + params, + id, + gpx, + previousShared, + family, + }); + await db.bulkDocs(family); + } + } catch (error) { + console.error({ + caller: 'putGpx', params, id, gpx, - previousShared, - family, + error, }); - await db.bulkDocs(family); } return id;