37 lines
1.8 KiB
TypeScript
37 lines
1.8 KiB
TypeScript
import { isRouterOsPreHeaderGetReset, MikrotikError } from "./mikrotik.js"
|
|
import { wireguardInterfaceDependencyPaths } from "./routeros-native-paths.js"
|
|
|
|
const cataloguePaths = new Set([
|
|
"/interface/wireguard", "/interface/wireguard/peers", "/ip/address", "/ipv6/address",
|
|
...wireguardInterfaceDependencyPaths,
|
|
])
|
|
|
|
type Category = "pre-header-reset" | "http-refusal" | "other-read-failure" | "invalid-catalogue" | "catalogue-limit" | "read-deadline"
|
|
type ReadDiagnostic = { catalogue: string; attempt?: 1 | 2; category: Category; httpStatus?: number; stage?: "read-back" }
|
|
const diagnostics = new WeakMap<object, ReadDiagnostic>()
|
|
|
|
export function markWireguardReadFailure<T extends Error>(error: T, path: string, attempt: 1 | 2 | undefined, category: Category): T {
|
|
if (cataloguePaths.has(path)) diagnostics.set(error, { catalogue: path, attempt, category })
|
|
return error
|
|
}
|
|
|
|
export function markWireguardGetFailure<T extends Error>(error: T, path: string, attempt: 1 | 2, cause: unknown): T {
|
|
const category = isRouterOsPreHeaderGetReset(cause) ? "pre-header-reset" : cause instanceof MikrotikError ? "http-refusal" : "other-read-failure"
|
|
markWireguardReadFailure(error, path, attempt, category)
|
|
const diagnostic = diagnostics.get(error)
|
|
if (diagnostic && cause instanceof MikrotikError && Number.isInteger(cause.statusCode) && cause.statusCode >= 400 && cause.statusCode <= 599) {
|
|
diagnostic.httpStatus = cause.statusCode
|
|
}
|
|
return error
|
|
}
|
|
|
|
export function wireguardReadDiagnostic(error: unknown): ReadDiagnostic | undefined {
|
|
return typeof error === "object" && error !== null ? diagnostics.get(error) : undefined
|
|
}
|
|
|
|
export function markWireguardReadBackFailure<T extends Error>(error: T, cause: unknown): T {
|
|
const diagnostic = wireguardReadDiagnostic(cause)
|
|
if (diagnostic) diagnostics.set(error, { ...diagnostic, stage: "read-back" })
|
|
return error
|
|
}
|