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