Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:2aronS Duel Agents Never Throw In Diagnostics

From Leeroopedia
Knowledge Sources
Domains Debugging, Error_Handling
Last Updated 2026-10-27 00:00 GMT

Overview

A connectivity/diagnostic probe must never throw — fold every failure (bad key, DNS error, timeout, refused connection) into a structured result object the caller can branch on.

Description

`checkConnectivity` is the only function in the Duel core that touches the network during install. Instead of throwing on a failed fetch, it catches every attempt, records the last error/status, and returns a `ConnectivityResult` with an `ok` boolean and a `status` discriminator (`"ok" | "unauthorized" | "unreachable" | "invalid_key"`). Invalid-key format is detected synchronously and returns immediately; 401/403 returns `unauthorized`; any other failure (DNS, timeout, refused, non-2xx) falls through to a final `unreachable` result with the last error message. The caller (`runDoctor`) then maps each status to an exit code and a human message, never a try/catch. This turns a diagnostic into a pure data value: the caller can `switch (result.status)` without a surrounding `try`.

Usage

Use this heuristic for any health check, connectivity probe, doctor command, or readiness check — anything whose job is to report the state of the world, not to perform an action. The rule: if the function's purpose is diagnosis, it returns a result; if its purpose is an action that can fail, it throws.

The Insight (Rule of Thumb)

  • Action: Wrap every I/O call in a try/catch; return a structured `{ ok, status, message, ... }` object instead of throwing.
  • Value: A discriminated `status` field (`"ok" | "unauthorized" | "unreachable" | "invalid_key"`) plus a human-readable `message` and optional `httpStatus`.
  • Trade-off: The caller must read the result instead of catching an error — slightly more code at the call site, but no exception-handling boilerplate and no risk of an unhandled rejection killing the diagnostic.
  • Compatibility: Distinguish local-detected failures (invalid key format) from network-detected failures (401) by using different `status` values, so the caller can give a precise fix.

Reasoning

A diagnostic that throws forces the caller to wrap it in try/catch just to report why the diagnostic failed — which inverts the purpose of a diagnostic. By returning a result, the probe becomes composable: `runDoctor` can `switch (result.status)` and map each case to an exit code, and a UI can render each status differently. The multi-endpoint fallback (it tries `/models`, then `/health`, then a `HEAD` on the base) only works because failures are non-fatal: each failed attempt updates `lastError`/`lastStatus` and the loop continues, so a single 404 on `/models` does not abort the probe.

Code Evidence

Structured result, no throw, from `packages/core/src/config.ts:174-242`:

export async function checkConnectivity(
  apiKey: string,
  proxyUrl = getProxyUrl(),
): Promise<ConnectivityResult> {
  if (!validateApiKey(apiKey)) {
    return {
      ok: false,
      status: "invalid_key",
      message: `Invalid API key format. Get a key at ${DASHBOARD_URL}`,
    };
  }
  // ... attempts loop, each failure caught:
    } catch (err) {
      lastError = err instanceof Error ? err.message : String(err);
    }
  // ... final fallback result:
  return {
    ok: false,
    status: "unreachable",
    httpStatus: lastStatus,
    message: lastError
      ? `Could not reach Duel API at ${base}: ${lastError}`
      : `Could not reach Duel API at ${base}. Check your network or try again later.`,
  };
}

Caller branches on status, no try/catch, from `packages/cli/src/install.ts:77-95`:

const result = await checkConnectivity(apiKey);
if (result.ok) {
  console.log(`✓ ${result.message}`);
  return 0;
}
if (result.status === "unauthorized") {
  console.error(`✗ ${result.message}`);
  return 1;
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment