fix(api): сообщать об отказах общего GRE-каталога
This commit is contained in:
1 parent
d112935c73
commit
6f00596e33
7 files changed
+65
-11
No files matched your search
@@ -0,0 +1,17 @@
|
||||
import assert from "node:assert/strict"
|
||||
import { pollGreCatalog } from "./gre-catalog-poll.js"
|
||||
const devices = [{ id: 1, name: "Первый" }, { id: 2, name: "Второй" }]
|
||||
const mixed = await pollGreCatalog(devices, async device => {
|
||||
if (device.id === 2) throw new Error("Таймаут тест")
|
||||
return { tunnels: [{ id: "*1", serverId: "1" }] }
|
||||
})
|
||||
assert.deepEqual(mixed, { tunnels: [{ id: "*1", serverId: "1" }], failures: [{ serverId: "2", serverName: "Второй", error: "Таймаут тест" }] })
|
||||
assert.deepEqual(await pollGreCatalog(devices, async () => ({ tunnels: [] })), { tunnels: [], failures: [] })
|
||||
const failed = await pollGreCatalog(devices, async () => { throw new Error("Недоступно") })
|
||||
assert.equal(failed.tunnels.length, 0)
|
||||
assert.deepEqual(failed.failures.map(f => f.serverId), ["1", "2"])
|
||||
const foreign = await pollGreCatalog([devices[0]], async () => ({ tunnels: [{ serverId: "2" }] }))
|
||||
assert.equal(foreign.tunnels.length, 0)
|
||||
assert.equal(foreign.failures[0].serverId, "1")
|
||||
assert.deepEqual(await pollGreCatalog([], async () => { throw new Error("Не должен вызываться") }), { tunnels: [], failures: [] })
|
||||
console.log("gre catalog: partial/empty/failed/foreign contexts passed")
|
||||
@@ -0,0 +1,14 @@
|
||||
export async function pollGreCatalog<S extends { id: number; name: string }, T extends { serverId: string }>(
|
||||
devices: readonly S[], read: (device: S) => Promise<{ tunnels: T[] }>,
|
||||
): Promise<{ tunnels: T[]; failures: Array<{ serverId: string; serverName: string; error: string }> }> {
|
||||
const results = await Promise.all(devices.map(async device => {
|
||||
try {
|
||||
const { tunnels } = await read(device)
|
||||
if (!Array.isArray(tunnels) || tunnels.some(t => t.serverId !== String(device.id))) throw new Error("Чужой контекст GRE-каталога")
|
||||
return { tunnels, failures: [] }
|
||||
} catch (error) {
|
||||
return { tunnels: [], failures: [{ serverId: String(device.id), serverName: device.name, error: error instanceof Error ? error.message : String(error) }] }
|
||||
}
|
||||
}))
|
||||
return { tunnels: results.flatMap(r => r.tunnels), failures: results.flatMap(r => r.failures) }
|
||||
}
|
||||
Reference in new issue
Block a user