Skip to content

keep-error-cause ​

A catch that creates a new error without passing the caught one on.

Why: the new error says what failed; the caught one says why. Without { cause: e } the original error and its stack are lost, which is usually exactly what you need when debugging (v8.dev). oxlint's preserve-caught-error covers throw; this rule covers errors that are returned, as err(...) values usually are.

✕ Rejectedkeep-error-cause
ts
import { err, type Result } from "@evobug_com/safe-ts-std/result";

class ConfigError extends Error {}

export function readPort(text: string): Result<number, ConfigError> {
  try {
    const port = Number(JSON.parse(text));
    return Number.isInteger(port) ? port : err(new ConfigError("port is not an integer"));
  } catch (e) {
    return err(new ConfigError("config is not valid JSON"));
  }
}

✅ Accepted: no catch at all. parseJson returns the failure, and a named error keeps it

✓ Accepted0 diagnostics
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { type JsonError, parseJson } from "@evobug_com/safe-ts-std/json";
import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

export type ConfigError = Enum<{ NotJson: { cause: JsonError }; NotInteger: {} }>;
export const ConfigError = constructors<ConfigError>();

export function readPort(text: string): Result<number, ConfigError> {
  const port = q(parseJson<number>(text), (cause) => ConfigError.NotJson({ cause }));
  return Number.isInteger(port) ? port : err(ConfigError.NotInteger());
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { parseJsonWith as __jsonParse, validateWith as __jsonValidate, jsonFail as __jsonFail, isJsonErr as __jsonIsErr, isJsonObject as __jsonIsObject, type JsonResult as __JsonResult } from "@evobug_com/safe-ts-std/json"; import { type JsonError, parseJson } from "@evobug_com/safe-ts-std/json";
import { Err as __qErr, mapErr as __qMapErr } from "@evobug_com/safe-ts-std/result"; import { err, q, type Result } from "@evobug_com/safe-ts-std/result";

export type ConfigError = Enum<{ NotJson: { cause: JsonError }; NotInteger: {} }>;
export const ConfigError = constructors<ConfigError>();

export function readPort(text: string): Result<number, ConfigError> {
  const __q1 = __jsonParse<number>(text, __va0); if (__q1 instanceof __qErr) return __qMapErr(__q1, (cause) => ({ tag: "NotJson", cause } satisfies ConfigError as ConfigError)); const port = __q1;
  return Number.isInteger(port) ? port : err(({ tag: "NotInteger" } satisfies ConfigError as ConfigError));
}
// ---- generated by safe-ts: JSON validators ----
function __va0(v: unknown, p: string): __JsonResult<number> {
  if (typeof v !== "number") return __jsonFail(p, "number", v);
  const out = v;
  return out;
}

✅ Also accepted: where the catch stays (around a package's code that throws), the new error passes the caught one on

✓ Accepted0 diagnostics
ts
import { err, type Result } from "@evobug_com/safe-ts-std/result";

class ConfigError extends Error {}

export function readPort(text: string): Result<number, ConfigError> {
  try {
    const port = Number(JSON.parse(text));
    return Number.isInteger(port) ? port : err(new ConfigError("port is not an integer"));
  } catch (e) {
    return err(new ConfigError("config is not valid JSON", { cause: e }));
  }
}

Quick fix: Keep e as the cause. The rule only looks at new ...Error(...) in a catch that doesn't use the caught value at all.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra