I have a very basic migration code that looks like this. It removes the table, creates the table, and seeds it with some data.
this.knex.schema.dropTable(this.tableName)
.catch((err) => console.log(err))
.then(() => {
this.knex.schema.createTable(this.tableName, function(table) {
table.increments("id").primary();
table.string("name");
table.integer("parent_id").unsigned().default(0);
})
.catch((err) => console.log(err))
.then(() => {
this.categories.forEach((category) => {
Category.create(category)
.catch((err) => console.log(err))
.then((category) => console.log(category.get("name") + " seeded."))
});
});
});
As you may notice, there is 3x .catch((err) => console.log(err))
chain on the code.
Right now I have Bugsnag integrated to my application and I want to make sure I properly log all the exceptions/errors on Bugsnag so I can fix all the bugs. However, right now all I can do is to log them into console. Even worse, I repeat myself and duplicating the logic in each catch
block.
I'm thinking about doing something like this:
.catch((err) => ErrorHandler.add(err))
class ErrorHandler {
add(err) {
// Notify Bugsnag
// Log it to console
}
}
That abrings another issue. What if I forget adding catch
methods... then it still won't work.
Thought about doing something like this, too:
// Change exception behaviour so whenever they are called, they raise an `onException` event
app.listen("onException", (err) => {
// Notify Bugsnag
// Log error to console
});
this way I can catch all errors and DRY my code, but I'm not sure if Node supports hooking exceptions.
What would you do in my case and what kind of approach should I take? I want to make sure all errors are properly sent to Bugsnag.