Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

764 B
Raw Blame
import { ProbeTimeoutError } from "./with-timeout";

const CODE_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$/;

/**
 * A short identifier for a failed probe. Driver messages can include hosts,
 * user names or credentials, so only well-formed codes and error names are
 * kept; anything else becomes UNKNOWN.
 */
export function safeErrorCode(error: unknown): string {
  if (error instanceof ProbeTimeoutError) return "TIMEOUT";
  if (typeof error !== "object" || error === null) return "UNKNOWN";
  const { code, name } = error as { code?: unknown; name?: unknown };
  if (typeof code === "string" && CODE_PATTERN.test(code)) return code;
  if (typeof name === "string" && name !== "Error" && CODE_PATTERN.test(name)) {
    return name;
  }
  return "UNKNOWN";
}