chore: standardize service domains and error logging
Build and Push Docker Images / detect-changes (push) Successful in 15s
Build and Push Docker Images / build-auth-api (push) Successful in 1m2s
Build and Push Docker Images / build-employee-api (push) Successful in 1m2s
Build and Push Docker Images / build-transit-api (push) Successful in 1m2s
Build and Push Docker Images / build-error-log-api (push) Successful in 1m10s
Build and Push Docker Images / build-admin-ui (push) Successful in 1m22s
Build and Push Docker Images / deploy-test-node (push) Successful in 4s
Build and Push Docker Images / detect-changes (push) Successful in 15s
Build and Push Docker Images / build-auth-api (push) Successful in 1m2s
Build and Push Docker Images / build-employee-api (push) Successful in 1m2s
Build and Push Docker Images / build-transit-api (push) Successful in 1m2s
Build and Push Docker Images / build-error-log-api (push) Successful in 1m10s
Build and Push Docker Images / build-admin-ui (push) Successful in 1m22s
Build and Push Docker Images / deploy-test-node (push) Successful in 4s
This commit is contained in:
+51
-39
@@ -1,29 +1,29 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const jwt = require('jsonwebtoken');
|
||||
require('dotenv').config();
|
||||
const express = require("express");
|
||||
const cors = require("cors");
|
||||
const jwt = require("jsonwebtoken");
|
||||
require("dotenv").config();
|
||||
|
||||
const db = require('./db');
|
||||
const db = require("./db");
|
||||
|
||||
const app = express();
|
||||
app.use(express.json({ limit: '64kb' }));
|
||||
app.use(express.json({ limit: "64kb" }));
|
||||
app.use(cors());
|
||||
|
||||
const PORT = Number(process.env.PORT || 5003);
|
||||
const JWT_SECRET = process.env.JWT_SECRET || '';
|
||||
const JWT_SECRET = process.env.JWT_SECRET || "";
|
||||
// Admin verification is done by `auth_api` and carried in the JWT as `isAdmin`.
|
||||
|
||||
function toFriendlyError(statusCode) {
|
||||
if (statusCode === 400) return 'Controleer je invoer en probeer opnieuw.';
|
||||
if (statusCode === 401) return 'Je sessie is verlopen. Log opnieuw in.';
|
||||
if (statusCode === 403) return 'Je hebt hier geen toegang toe.';
|
||||
if (statusCode === 404) return 'Niet gevonden.';
|
||||
return 'Er ging iets mis. Probeer het later opnieuw.';
|
||||
if (statusCode === 400) return "Controleer je invoer en probeer opnieuw.";
|
||||
if (statusCode === 401) return "Je sessie is verlopen. Log opnieuw in.";
|
||||
if (statusCode === 403) return "Je hebt hier geen toegang toe.";
|
||||
if (statusCode === 404) return "Niet gevonden.";
|
||||
return "Er ging iets mis. Probeer het later opnieuw.";
|
||||
}
|
||||
|
||||
function normalizeErrorText(value) {
|
||||
if (!value) return 'Unknown error';
|
||||
if (typeof value === 'string') return value;
|
||||
if (!value) return "Unknown error";
|
||||
if (typeof value === "string") return value;
|
||||
try {
|
||||
return JSON.stringify(value);
|
||||
} catch (_) {
|
||||
@@ -31,20 +31,28 @@ function normalizeErrorText(value) {
|
||||
}
|
||||
}
|
||||
|
||||
async function insertErrorLog({ service, partOfService, error }) {
|
||||
const serviceName = String(service || 'unknown_service').slice(0, 120);
|
||||
const part = partOfService ? String(partOfService).slice(0, 240) : null;
|
||||
async function insertErrorLog({
|
||||
service,
|
||||
partofservice,
|
||||
partOfService,
|
||||
error,
|
||||
}) {
|
||||
const serviceName = String(service || "unknown_service").slice(0, 120);
|
||||
const part =
|
||||
partofservice || partOfService
|
||||
? String(partofservice || partOfService).slice(0, 240)
|
||||
: null;
|
||||
const errorText = String(normalizeErrorText(error)).slice(0, 8000);
|
||||
|
||||
await db.query(
|
||||
'INSERT INTO data.errors ("service", "partOfService", "error") VALUES ($1, $2, $3)',
|
||||
[serviceName, part, errorText]
|
||||
"INSERT INTO data.errors (service, partofservice, error) VALUES ($1, $2, $3)",
|
||||
[serviceName, part, errorText],
|
||||
);
|
||||
}
|
||||
|
||||
function verifyAdmin(req, res, next) {
|
||||
const auth = req.headers.authorization;
|
||||
if (!auth || !auth.startsWith('Bearer ')) {
|
||||
if (!auth || !auth.startsWith("Bearer ")) {
|
||||
return res.status(401).json({ error: toFriendlyError(401) });
|
||||
}
|
||||
|
||||
@@ -61,33 +69,37 @@ function verifyAdmin(req, res, next) {
|
||||
}
|
||||
}
|
||||
|
||||
app.get('/health', async (_req, res) => {
|
||||
res.json({ status: 'ok', service: 'error_log_api' });
|
||||
app.get("/health", async (_req, res) => {
|
||||
res.json({ status: "ok", service: "error_log_api" });
|
||||
});
|
||||
|
||||
app.post('/error_log', async (req, res) => {
|
||||
app.post("/error_log", async (req, res) => {
|
||||
try {
|
||||
const service = req.body?.service;
|
||||
const partOfService = req.body?.partOfService || null;
|
||||
const partofservice =
|
||||
req.body?.partofservice || req.body?.partOfService || null;
|
||||
const error = req.body?.error;
|
||||
|
||||
if (!service || !error) {
|
||||
return res.status(400).json({ error: toFriendlyError(400) });
|
||||
}
|
||||
|
||||
await insertErrorLog({ service, partOfService, error });
|
||||
await insertErrorLog({ service, partofservice, error });
|
||||
return res.status(201).json({ ok: true });
|
||||
} catch (err) {
|
||||
console.error('POST /error_log failed:', err);
|
||||
console.error("POST /error_log failed:", err);
|
||||
return res.status(500).json({ error: toFriendlyError(500) });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/error_log', verifyAdmin, async (req, res) => {
|
||||
app.get("/error_log", verifyAdmin, async (req, res) => {
|
||||
try {
|
||||
const limit = Math.min(Number(req.query.limit || 100), 500);
|
||||
const service = req.query.service ? String(req.query.service) : null;
|
||||
const partOfService = req.query.partOfService ? String(req.query.partOfService) : null;
|
||||
const partofservice =
|
||||
req.query.partofservice || req.query.partOfService
|
||||
? String(req.query.partofservice || req.query.partOfService)
|
||||
: null;
|
||||
|
||||
const where = [];
|
||||
const params = [];
|
||||
@@ -96,25 +108,25 @@ app.get('/error_log', verifyAdmin, async (req, res) => {
|
||||
params.push(service);
|
||||
where.push(`"service" = $${params.length}`);
|
||||
}
|
||||
if (partOfService) {
|
||||
params.push(partOfService);
|
||||
where.push(`"partOfService" ILIKE $${params.length}`);
|
||||
params[params.length - 1] = `%${partOfService}%`;
|
||||
if (partofservice) {
|
||||
params.push(partofservice);
|
||||
where.push(`partofservice ILIKE $${params.length}`);
|
||||
params[params.length - 1] = `%${partofservice}%`;
|
||||
}
|
||||
|
||||
params.push(limit);
|
||||
const rows = await db.query(
|
||||
`SELECT id, "date", "service", "partOfService", "error"
|
||||
`SELECT id, date, service, partofservice, error
|
||||
FROM data.errors
|
||||
${where.length ? `WHERE ${where.join(' AND ')}` : ''}
|
||||
${where.length ? `WHERE ${where.join(" AND ")}` : ""}
|
||||
ORDER BY "date" DESC, id DESC
|
||||
LIMIT $${params.length}`,
|
||||
params
|
||||
params,
|
||||
);
|
||||
|
||||
return res.json({ errors: rows.rows });
|
||||
} catch (err) {
|
||||
console.error('GET /error_log failed:', err);
|
||||
console.error("GET /error_log failed:", err);
|
||||
return res.status(500).json({ error: toFriendlyError(500) });
|
||||
}
|
||||
});
|
||||
@@ -122,12 +134,12 @@ app.get('/error_log', verifyAdmin, async (req, res) => {
|
||||
app.use(async (err, req, res, _next) => {
|
||||
try {
|
||||
await insertErrorLog({
|
||||
service: 'error_log_api',
|
||||
partOfService: `${req.method} ${req.originalUrl}`,
|
||||
service: "error_log_api",
|
||||
partofservice: `${req.method} ${req.originalUrl}`,
|
||||
error: err?.stack || err?.message || String(err),
|
||||
});
|
||||
} catch (logErr) {
|
||||
console.error('Failed to log internal error:', logErr);
|
||||
console.error("Failed to log internal error:", logErr);
|
||||
}
|
||||
|
||||
if (res.headersSent) return;
|
||||
|
||||
Reference in New Issue
Block a user