feat(MikrotikConfig): enhance applyMikrotikConfig and NetworkConfigManager with detailed request logging and error handling improvements
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 1m57s
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 1m57s
This commit is contained in:
@@ -238,8 +238,11 @@ const APPLY_TIMEOUT_MS = Number(process.env.MIKROTIK_APPLY_TIMEOUT_MS) || 60000;
|
||||
*/
|
||||
async function applyMikrotikConfig(req, res) {
|
||||
let conn = null;
|
||||
const mikrotikRequests = [];
|
||||
const body = req.body || {};
|
||||
console.log('[MikroTik apply] Request body:', { serverId: body.serverId, type: body.type, dryRun: body.dryRun });
|
||||
try {
|
||||
const { serverId, type = 'all', dryRun = true } = req.body || {};
|
||||
const { serverId, type = 'all', dryRun = true } = body;
|
||||
if (!serverId) {
|
||||
return sendError(res, 400, 'serverId is required', 'E_BAD_REQUEST');
|
||||
}
|
||||
@@ -290,7 +293,15 @@ async function applyMikrotikConfig(req, res) {
|
||||
port: Number(port) || 8728,
|
||||
});
|
||||
try {
|
||||
console.log('[MikroTik apply] Connecting to', host + ':' + port, 'user:', user);
|
||||
await conn.connect();
|
||||
const originalWrite = conn.write.bind(conn);
|
||||
conn.write = async function (path, args) {
|
||||
const req = { path, args: args || [] };
|
||||
mikrotikRequests.push(req);
|
||||
console.log('[MikroTik API]', path, req.args);
|
||||
return originalWrite(path, args);
|
||||
};
|
||||
const allResults = [];
|
||||
for (const block of blocks) {
|
||||
const blockResults = await applyBlock(conn, block, !!dryRun);
|
||||
@@ -308,7 +319,7 @@ async function applyMikrotikConfig(req, res) {
|
||||
skipped: allResults.flatMap(b => b.results).filter(r => r.status === 'skip').length,
|
||||
errors: allResults.flatMap(b => b.results).filter(r => r.status === 'error'),
|
||||
};
|
||||
return { ok: true, dryRun: !!dryRun, summary, results: allResults };
|
||||
return { ok: true, dryRun: !!dryRun, summary, results: allResults, mikrotikRequests };
|
||||
} catch (err) {
|
||||
if (conn) try { conn.close(); } catch (_) {}
|
||||
conn = null;
|
||||
@@ -326,12 +337,14 @@ async function applyMikrotikConfig(req, res) {
|
||||
if (conn) try { conn.close(); } catch (_) {}
|
||||
console.error('Error applying MikroTik config:', error);
|
||||
const msg = error.message || String(error);
|
||||
return res.status(500).json({
|
||||
const errPayload = {
|
||||
ok: false,
|
||||
error: msg.includes('ECONNREFUSED') ? 'Соединение отклонено. Проверьте хост и порт.' :
|
||||
msg.includes('ETIMEDOUT') || msg.includes('Таймаут') ? msg :
|
||||
msg.includes('Authentication') || msg.includes('login') ? 'Неверный логин или пароль' : msg,
|
||||
});
|
||||
};
|
||||
if (mikrotikRequests.length > 0) errPayload.mikrotikRequests = mikrotikRequests;
|
||||
return res.status(500).json(errPayload);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user