How to access error events -- Is there a best/good practice for error handling

I'm writing my methods and associated tests for db access and when trying to validate error events. I get a SQL event log but nothing is caught in my try-catch block. When I log the response it is either empty or undefined. *Example
export const postLog = async ({
tableName,
data,
}: {
tableName: string;
data: {
day: string;
log: string;
};
}) => {
try {
const results = await db.insertInto(tableName).values(data).execute();
console.log(results)
return results;
} catch (err) {
console.error('ERROR: in postlog');
console.error(err);
return err;
}
};
export const postLog = async ({
tableName,
data,
}: {
tableName: string;
data: {
day: string;
log: string;
};
}) => {
try {
const results = await db.insertInto(tableName).values(data).execute();
console.log(results)
return results;
} catch (err) {
console.error('ERROR: in postlog');
console.error(err);
return err;
}
};
I don't see in the docs where expected errors are defined. Thank you for your time. Cheers,
2 Replies
bombillazo
bombillazo10mo ago
Try
...execute().catch((err)=>{
console.error(err)
})
...execute().catch((err)=>{
console.error(err)
})
aaizulcpcso
aaizulcpcso10mo ago
Thank you for the response. This doesn't seem to work. I attempted to create the same table twice and it logs and error but does not throw and error or return and error object for work with. To me, this implies that I will have to write checks or come up with some other error handling pattern.
let newTable = db.schema
.createTable(tableName);


columns.forEach((c) => {
console.log(c);
newTable = newTable.addColumn(c.name, c.type, (col) => {
if (c.isUnique) {
return col.notNull().unique();
}
return col;
});
});
console.log(newTable);
await newTable.execute().catch((err) => {
cliLogger.error('ERROR - creating new table');
console.log(err);
});
let newTable = db.schema
.createTable(tableName);


columns.forEach((c) => {
console.log(c);
newTable = newTable.addColumn(c.name, c.type, (col) => {
if (c.isUnique) {
return col.notNull().unique();
}
return col;
});
});
console.log(newTable);
await newTable.execute().catch((err) => {
cliLogger.error('ERROR - creating new table');
console.log(err);
});
What are other people/teams doing for tests and error handling coverage?