mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 15:11:31 +08:00 
			
		
		
		
	better error handling / logging of sync events
This commit is contained in:
		
							parent
							
								
									7af6b69331
								
							
						
					
					
						commit
						0e8fcbc0f5
					
				@ -24,13 +24,14 @@ async function pullSync(cookieJar, syncLog) {
 | 
				
			|||||||
        syncRows = await rp({
 | 
					        syncRows = await rp({
 | 
				
			||||||
            uri: SYNC_SERVER + '/api/sync/changed?lastSyncId=' + lastSyncedPull + "&sourceId=" + SOURCE_ID,
 | 
					            uri: SYNC_SERVER + '/api/sync/changed?lastSyncId=' + lastSyncedPull + "&sourceId=" + SOURCE_ID,
 | 
				
			||||||
            jar: cookieJar,
 | 
					            jar: cookieJar,
 | 
				
			||||||
            json: true
 | 
					            json: true,
 | 
				
			||||||
 | 
					            timeout: 5 * 1000
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        logSync("Pulled " + syncRows.length + " changes");
 | 
					        logSync("Pulled " + syncRows.length + " changes");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    catch (e) {
 | 
					    catch (e) {
 | 
				
			||||||
        throw new Error("Can't pull changes, inner exception: " + e.stack);
 | 
					        logSyncError("Can't pull changes, inner exception: ", e, syncLog);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const sync of syncRows) {
 | 
					    for (const sync of syncRows) {
 | 
				
			||||||
@ -44,7 +45,7 @@ async function pullSync(cookieJar, syncLog) {
 | 
				
			|||||||
            });
 | 
					            });
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        catch (e) {
 | 
					        catch (e) {
 | 
				
			||||||
            throw new Error("Can't pull " + sync.entity_name + " " + sync.entity_id + ", inner exception: " + e.stack);
 | 
					            logSyncError("Can't pull " + sync.entity_name + " " + sync.entity_id, e, syncLog);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // TODO: ideally this should be in transaction
 | 
					        // TODO: ideally this should be in transaction
 | 
				
			||||||
@ -59,9 +60,7 @@ async function pullSync(cookieJar, syncLog) {
 | 
				
			|||||||
            await updateNoteHistory(resp, sync.source_id, syncLog);
 | 
					            await updateNoteHistory(resp, sync.source_id, syncLog);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else {
 | 
					        else {
 | 
				
			||||||
            logSync("Unrecognized entity type " + sync.entity_name, syncLog);
 | 
					            logSyncError("Unrecognized entity type " + sync.entity_name, e, syncLog);
 | 
				
			||||||
 | 
					 | 
				
			||||||
            throw new Error("Unrecognized entity type " + sync.entity_name);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        await sql.setOption('last_synced_pull', sync.id);
 | 
					        await sql.setOption('last_synced_pull', sync.id);
 | 
				
			||||||
@ -86,14 +85,12 @@ async function syncEntity(entity, entityName, cookieJar, syncLog) {
 | 
				
			|||||||
            uri: SYNC_SERVER + '/api/sync/' + entityName,
 | 
					            uri: SYNC_SERVER + '/api/sync/' + entityName,
 | 
				
			||||||
            body: payload,
 | 
					            body: payload,
 | 
				
			||||||
            json: true,
 | 
					            json: true,
 | 
				
			||||||
            timeout: 60 * 1000,
 | 
					            timeout: 5 * 1000,
 | 
				
			||||||
            jar: cookieJar
 | 
					            jar: cookieJar
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    catch (e) {
 | 
					    catch (e) {
 | 
				
			||||||
        logSync("Failed sending update for entity " + entityName + ", inner exception: " + e.stack, syncLog);
 | 
					        logSyncError("Failed sending update for entity " + entityName, e, syncLog);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        throw new Error("Failed sending update for entity " + entityName + ", inner exception: " + e.stack);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -124,9 +121,7 @@ async function pushSync(cookieJar, syncLog) {
 | 
				
			|||||||
                entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]);
 | 
					                entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            else {
 | 
					            else {
 | 
				
			||||||
                logSync("Unrecognized entity type " + sync.entity_name, syncLog);
 | 
					                logSyncError("Unrecognized entity type " + sync.entity_name, null, syncLog);
 | 
				
			||||||
 | 
					 | 
				
			||||||
                throw new Error("Unrecognized entity type " + sync.entity_name);
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            await syncEntity(entity, sync.entity_name, cookieJar, syncLog);
 | 
					            await syncEntity(entity, sync.entity_name, cookieJar, syncLog);
 | 
				
			||||||
@ -138,7 +133,7 @@ async function pushSync(cookieJar, syncLog) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function login() {
 | 
					async function login(syncLog) {
 | 
				
			||||||
    const timestamp = utils.nowTimestamp();
 | 
					    const timestamp = utils.nowTimestamp();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const documentSecret = await sql.getOption('document_secret');
 | 
					    const documentSecret = await sql.getOption('document_secret');
 | 
				
			||||||
@ -163,7 +158,7 @@ async function login() {
 | 
				
			|||||||
        return cookieJar;
 | 
					        return cookieJar;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    catch (e) {
 | 
					    catch (e) {
 | 
				
			||||||
        throw new Error("Can't login to API for sync, inner exception: " + e.stack);
 | 
					        logSyncError("Can't login to API for sync, inner exception: ", e, syncLog);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -185,7 +180,7 @@ async function sync() {
 | 
				
			|||||||
            return syncLog;
 | 
					            return syncLog;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const cookieJar = await login();
 | 
					        const cookieJar = await login(syncLog);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        await pushSync(cookieJar, syncLog);
 | 
					        await pushSync(cookieJar, syncLog);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -209,6 +204,22 @@ function logSync(message, syncLog) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function logSyncError(message, e, syncLog) {
 | 
				
			||||||
 | 
					    let completeMessage = message;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (e) {
 | 
				
			||||||
 | 
					        completeMessage += ", inner exception: " + e.stack;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log.info(completeMessage);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (syncLog) {
 | 
				
			||||||
 | 
					        syncLog.push(completeMessage);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    throw new Error(completeMessage);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getChanged(lastSyncId, sourceId) {
 | 
					async function getChanged(lastSyncId, sourceId) {
 | 
				
			||||||
    console.log([lastSyncId, sourceId]);
 | 
					    console.log([lastSyncId, sourceId]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user