Skip to content

Error Handling

Kaelum provides a global error handler that catches unhandled exceptions across all routes and middleware.

app.useErrorHandler();
app.useErrorHandler({ exposeStack: false });
  • Captures unhandled exceptions in routes or middleware
  • Returns JSON for API endpoints (when Accept: application/json)
  • Returns HTML for web page requests
  • Must be called after all routes and middleware

Kaelum wraps all route handlers in a try/catch. Async errors (promise rejections) are automatically caught and passed to Express’s next(err):

app.addRoute("/users", {
get: async (req, res) => {
const users = await db.getUsers(); // if this throws, it's caught
res.json(users);
}
});

No need for manual try/catch in every async handler.

For web apps, you can customize the error response by adding your own error middleware before calling useErrorHandler():

app.use((err, req, res, next) => {
if (req.accepts("html")) {
res.status(500).render("error", { message: err.message });
} else {
next(err); // fallback to Kaelum's handler
}
});
app.useErrorHandler();