21 lines
1.3 KiB
TypeScript
21 lines
1.3 KiB
TypeScript
import { MikrotikError } from "../../services/mikrotik.js"
|
|
|
|
/** Acceptance diagnostics only: never retain messages, bodies, headers or request arguments. */
|
|
export function chrNativeFailure(error: unknown, method: string, path: string, startedAt: number) {
|
|
const text = error instanceof Error ? error.message : ""
|
|
const code = error instanceof Error ? (error as NodeJS.ErrnoException).code : undefined
|
|
const reason = error instanceof MikrotikError ? "http"
|
|
: code === "ECONNRESET" ? "connection_reset"
|
|
: code === "ECONNREFUSED" ? "connection_refused"
|
|
: code === "ENOTFOUND" || code === "EAI_AGAIN" ? "dns"
|
|
: code === "ETIMEDOUT" || /timed out|timeout/i.test(text) ? "timeout"
|
|
: error instanceof Error && error.name === "AbortError" ? "aborted" : "transport_unknown"
|
|
return {
|
|
phase: "native-request-failure", at: new Date().toISOString(),
|
|
method: ["GET", "POST", "PUT", "PATCH", "DELETE"].includes(method) ? method : "unknown",
|
|
path: /^\/(?:routing|ip|ipv6|interface|system|tool|ping)(?:\/[a-z0-9_.*-]+)*$/i.test(path) && path.length <= 300 ? path : "[unclassified]",
|
|
reason, ...(error instanceof MikrotikError ? { nativeStatus: error.statusCode } : {}),
|
|
elapsedMs: Math.max(0, Date.now() - startedAt), automaticRetry: false,
|
|
}
|
|
}
|