feat: add centralized error logging system
- Implemented Error Log API to capture and store error logs from various services. - Updated admin_ui to include Error Log Viewer component for inspecting logs. - Enhanced error handling in auth_api, employee_api, and transit_api to report errors to the Error Log API. - Modified existing services to utilize the new error logging functionality. - Updated Docker Compose configuration to include the Error Log API service. - Added error logging service in bus_terminal for Flutter application.
This commit is contained in:
+51
-4
@@ -18,7 +18,51 @@ app.use((req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
const ERROR_LOG_API_URL = process.env.ERROR_LOG_API_URL || 'http://localhost:5003/error_log';
|
||||
|
||||
function friendlyErrorMessage(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.';
|
||||
}
|
||||
|
||||
async function reportError({ partOfService, error }) {
|
||||
try {
|
||||
await fetch(ERROR_LOG_API_URL, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
service: 'auth_api',
|
||||
partOfService,
|
||||
error,
|
||||
}),
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
app.use((req, res, next) => {
|
||||
const originalJson = res.json.bind(res);
|
||||
res.json = (body) => {
|
||||
if (res.statusCode >= 400 && body && typeof body === 'object') {
|
||||
const rawError = body.error || body.message || body.details || `HTTP ${res.statusCode}`;
|
||||
void reportError({ partOfService: `${req.method} ${req.originalUrl}`, error: rawError });
|
||||
return originalJson({ error: friendlyErrorMessage(res.statusCode) });
|
||||
}
|
||||
return originalJson(body);
|
||||
};
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET;
|
||||
const ADMIN_USER_CODES = new Set(
|
||||
String(process.env.ADMIN_USER_CODES || '')
|
||||
.split(',')
|
||||
.map(c => Number(c.trim()))
|
||||
.filter(Number.isFinite)
|
||||
);
|
||||
|
||||
|
||||
app.post('/login', async (req, res) => {
|
||||
@@ -33,13 +77,15 @@ app.post('/login', async (req, res) => {
|
||||
const ok = await bcrypt.compare(password, user.password);
|
||||
if (!ok) return res.status(401).json({ error: 'invalid_credentials' });
|
||||
|
||||
const payload = { userId: user.id, userCode: user.usercode };
|
||||
const isAdmin = ADMIN_USER_CODES.has(Number(user.usercode));
|
||||
const payload = { userId: user.id, userCode: user.usercode, isAdmin };
|
||||
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: '1h' });
|
||||
|
||||
res.json({ token, user: { id: user.id, userCode: user.userCode, job: user.job } });
|
||||
res.json({ token, user: { id: user.id, userCode: user.usercode, job: user.job, isAdmin } });
|
||||
} catch (err) {
|
||||
await reportError({ partOfService: `${req.method} ${req.originalUrl}`, error: err?.stack || err?.message || String(err) });
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'internal_error' });
|
||||
res.status(500).json({ error: friendlyErrorMessage(500) });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -51,7 +97,8 @@ app.get('/me', async (req, res) => {
|
||||
const payload = jwt.verify(token, JWT_SECRET);
|
||||
res.json({ user: payload });
|
||||
} catch (err) {
|
||||
res.status(401).json({ error: 'invalid_token' });
|
||||
void reportError({ partOfService: `${req.method} ${req.originalUrl}`, error: err?.stack || err?.message || String(err) });
|
||||
res.status(401).json({ error: friendlyErrorMessage(401) });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user