Add comprehensive local logging to all microservices; log all errors locally and forward to error_log_api
This commit is contained in:
+11
-9
@@ -51,6 +51,7 @@ app.use((req, res, next) => {
|
||||
if (res.statusCode >= 400 && body && typeof body === "object") {
|
||||
const rawError =
|
||||
body.error || body.message || body.details || `HTTP ${res.statusCode}`;
|
||||
log("warn", `HTTP ${res.statusCode}`, { path: req.originalUrl, method: req.method, error: rawError });
|
||||
void reportError({
|
||||
partofservice: `${req.method} ${req.originalUrl}`,
|
||||
error: rawError,
|
||||
@@ -67,10 +68,12 @@ app.use((req, res, next) => {
|
||||
app.post("/createemployee", async (req, res) => {
|
||||
try {
|
||||
const { firstname, lastname, job, password } = req.body;
|
||||
if (!firstname || !lastname || !job || !password)
|
||||
if (!firstname || !lastname || !job || !password) {
|
||||
log("warn", "Missing required fields in createemployee", { body: req.body });
|
||||
return res.status(400).json({
|
||||
error: "Firstname, Lastname, Job title and password are required",
|
||||
});
|
||||
}
|
||||
|
||||
const resultCreateEmployee = await db.query(
|
||||
"INSERT INTO workers.employees (firstname, lastname, job) VALUES ($1, $2, $3) RETURNING id",
|
||||
@@ -83,15 +86,13 @@ app.post("/createemployee", async (req, res) => {
|
||||
[newEmployee.id, encryptedpassword],
|
||||
);
|
||||
const newUser = resultCreateUser.rows[0];
|
||||
log("info", "Employee created", { employeeId: newEmployee.id, userCode: newUser.usercode });
|
||||
return res
|
||||
.status(201)
|
||||
.json({ employee: firstname, code: newUser.usercoden });
|
||||
.json({ employee: firstname, code: newUser.usercode });
|
||||
} catch (err) {
|
||||
void reportError({
|
||||
partofservice: `${req.method} ${req.originalUrl}`,
|
||||
error: err?.stack || err?.message || String(err),
|
||||
});
|
||||
console.error(err);
|
||||
log("error", "Create employee error", { error: err.message, stack: err.stack });
|
||||
reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message });
|
||||
res.status(500).json({ error: friendlyErrorMessage(500) });
|
||||
}
|
||||
});
|
||||
@@ -101,12 +102,13 @@ app.get("/health", async (_req, res) => {
|
||||
await db.query("SELECT 1");
|
||||
return res.status(200).json({ endpoint: "reachable", database: "ok" });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
log("error", "Health check DB query failed", { error: err.message, stack: err.stack });
|
||||
reportError({ partofservice: "GET /health", error: err.stack || err.message });
|
||||
res.status(503).json({ endpoint: "unreachable", database: "down" });
|
||||
}
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 4000;
|
||||
app.listen(PORT, () => console.log(`Auth API listening on ${PORT}`));
|
||||
app.listen(PORT, () => console.log(`Employee API listening on ${PORT}`));
|
||||
|
||||
module.exports = app;
|
||||
|
||||
Reference in New Issue
Block a user