add extra error handling
This commit is contained in:
		
							parent
							
								
									5d0cd02c21
								
							
						
					
					
						commit
						7298023fb9
					
				@ -60,31 +60,42 @@ try {
 | 
				
			|||||||
// Upload logic
 | 
					// Upload logic
 | 
				
			||||||
let lastData: Record<string, any> = {};
 | 
					let lastData: Record<string, any> = {};
 | 
				
			||||||
async function _upload() {
 | 
					async function _upload() {
 | 
				
			||||||
	console.log('-- Reading log...');
 | 
						try {
 | 
				
			||||||
	const reader = new MDBReader(await readFile(dbPath));
 | 
							console.log('-- Reading log...');
 | 
				
			||||||
	if (!reader.getTableNames().includes('tblContacts'))
 | 
							const reader = new MDBReader(await readFile(dbPath));
 | 
				
			||||||
		return console.log('!! No "tblContacts" table present.');
 | 
							if (!reader.getTableNames().includes('tblContacts'))
 | 
				
			||||||
 | 
								return console.log('!! No "tblContacts" table present.');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const table = reader.getTable("tblContacts");
 | 
							const table = reader.getTable("tblContacts");
 | 
				
			||||||
	const tableData = table.getData<Record<string, string>>();
 | 
							const tableData = table.getData<Record<string, string>>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const data = tableData.reduce((p, r) => ({ ...p, [r.fldPrimaryKey]: r }), {} as Record<string, any>);
 | 
							const data = tableData.reduce((p, r) => ({ ...p, [r.fldPrimaryKey]: r }), {} as Record<string, any>);
 | 
				
			||||||
	const differences = diff(lastData, data);
 | 
							const differences = diff(lastData, data);
 | 
				
			||||||
	const changedIds = [...new Set(differences.filter((r) => r.op !== 'remove').map((r) => r.path[0]))];
 | 
							const changedIds = [...new Set(differences.filter((r) => r.op !== 'remove').map((r) => r.path[0]))];
 | 
				
			||||||
	const changedRows = changedIds.map((id) => data[id]);
 | 
							const changedRows = changedIds.map((id) => data[id]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (changedIds.length === 0) return void console.log('-- No changes found.');
 | 
							if (changedIds.length === 0) return void console.log('-- No changes found.');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	console.log(`-> Uploading ${changedIds.length.toLocaleString()} row(s)...`);
 | 
							try {
 | 
				
			||||||
	const response = await fetch(logEndpoint, {
 | 
					
 | 
				
			||||||
		method: 'POST',
 | 
								console.log(`-> Uploading ${changedIds.length.toLocaleString()} row(s)...`);
 | 
				
			||||||
		body: JSON.stringify(changedRows),
 | 
								const response = await fetch(logEndpoint, {
 | 
				
			||||||
		headers: {
 | 
									method: 'POST',
 | 
				
			||||||
			'Content-Type': 'application/json'
 | 
									body: JSON.stringify(changedRows),
 | 
				
			||||||
 | 
									headers: {
 | 
				
			||||||
 | 
										'Content-Type': 'application/json'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
 | 
								console.log(`<- Recieved ${response.status} (${response.statusText})`);
 | 
				
			||||||
 | 
								if (response.status === 200) lastData = data;
 | 
				
			||||||
 | 
							} catch (e) {
 | 
				
			||||||
 | 
								console.log('-! Failed to send log');
 | 
				
			||||||
 | 
								console.error(e);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	});
 | 
						} catch (e) {
 | 
				
			||||||
	console.log(`<- Recieved ${response.status} (${response.statusText})`);
 | 
							console.log('-! Failed to upload');
 | 
				
			||||||
	if (response.status === 200) lastData = data;
 | 
							console.error(e);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const upload = debounce(_upload, 250);
 | 
					const upload = debounce(_upload, 250);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user