Skip to content

catch-on-result ​

.catch(...) (or .then(ok, fail)) on a call of the project's async function that returns Promise<Result<...>>, and a try/catch around nothing but calls of functions that return a Result. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.

Why: a checked function that returns a Result reports its failures in it; it doesn't throw or reject (a throw would be a bug the checker reports where it starts). When a store member changes from rejecting to returning a Result, the callers that kept their .catch or try still compile and still pass, and the failure, now in the Result, is dropped. Check the Result instead. Guide: Errors.

❌ Rejected (the Result is only logged, so must-use-result reports it too)

ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";

export type DeployError = Enum<{ NoProject: {}; Refused: { reason: string } }>;
export const DeployError = constructors<DeployError>();

export async function deploy(id: string): Promise<Result<string, DeployError>> {
  return id === "" ? err(DeployError.NoProject()) : `deployed ${id}`;
}

export async function onDeploy(id: string, toast: (text: string) => void): Promise<void> {
  try {
    const done = await deploy(id);
    console.log(done);
  } catch {
    toast("Deploy failed");
  }
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";

export type DeployError = Enum<{ NoProject: {}; Refused: { reason: string } }>;
export const DeployError = constructors<DeployError>();

export async function deploy(id: string): Promise<Result<string, DeployError>> {
  return id === "" ? err(({ tag: "NoProject" } satisfies DeployError as DeployError)) : `deployed ${id}`;
}

export async function onDeploy(id: string, toast: (text: string) => void): Promise<void> {
  try {
    const done = await deploy(id);
    console.log(done);
  } catch {
    toast("Deploy failed");
  }
}
✓ Accepted0 diagnostics
ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";

export type DeployError = Enum<{ NoProject: {}; Refused: { reason: string } }>;
export const DeployError = constructors<DeployError>();

export async function deploy(id: string): Promise<Result<string, DeployError>> {
  return id === "" ? err(DeployError.NoProject()) : `deployed ${id}`;
}

export async function onDeploy(id: string, toast: (text: string) => void): Promise<void> {
  const done = await deploy(id);
  if (!isErr(done)) return;
  toast(
    match(done.error, {
      NoProject: () => "Pick a project first",
      Refused: ({ reason }) => `Deploy refused: ${reason}`,
    }),
  );
}
ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";

export type DeployError = Enum<{ NoProject: {}; Refused: { reason: string } }>;
export const DeployError = constructors<DeployError>();

export async function deploy(id: string): Promise<Result<string, DeployError>> {
  return id === "" ? err(({ tag: "NoProject" } satisfies DeployError as DeployError)) : `deployed ${id}`;
}

export async function onDeploy(id: string, toast: (text: string) => void): Promise<void> {
  const done = await deploy(id);
  if (!isErr(done)) return;
  toast(
    match(done.error, {
      NoProject: () => "Pick a project first",
      Refused: ({ reason }) => `Deploy refused: ${reason}`,
    }),
  );
}

A try that is a PropagateError boundary isn't reported: a function that returns a Result for the failures callers handle can also throw a tracked PropagateError for the ones they can't (a corrupt save at the first bad byte). When a call in the try can throw one that this catch takes, the catch runs. A catch that lets that kind through (if (e instanceof Other) ...; throw e) is still reported:

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

export class CorruptSave extends PropagateError {}

export type SaveError = Enum<{ Empty: {} }>;
export const SaveError = constructors<SaveError>();

/** The level a save stores. */
export function readSave(bytes: string): Result<number, SaveError> {
  if (bytes === "") return err(SaveError.Empty());
  if (bytes.includes("\u0000")) throw new CorruptSave("bad byte");
  return bytes.length;
}

/** The level to start at: without a usable save, a game starts at level 1. */
export function startLevel(bytes: string): number {
  try {
    const level = readSave(bytes);
    return isErr(level) ? 1 : level;
  } catch (error) {
    if (!(error instanceof CorruptSave)) throw error;
    console.warn(error.message);
    return 1;
  }
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, PropagateError, type Result } from "@evobug_com/safe-ts-std/result";

export class CorruptSave extends PropagateError {}

export type SaveError = Enum<{ Empty: {} }>;
export const SaveError = constructors<SaveError>();

/** The level a save stores. */
export function readSave(bytes: string): Result<number, SaveError> {
  if (bytes === "") return err(({ tag: "Empty" } satisfies SaveError as SaveError));
  if (bytes.includes("\u0000")) throw new CorruptSave("bad byte");
  return bytes.length;
}

/** The level to start at: without a usable save, a game starts at level 1. */
export function startLevel(bytes: string): number {
  try {
    const level = readSave(bytes);
    return isErr(level) ? 1 : level;
  } catch (error) {
    if (!(error instanceof CorruptSave)) throw error;
    console.warn(error.message);
    return 1;
  }
}

A try that also calls something the checker can't follow (a method of a value from a package, JSON.parse, new ...) isn't reported; console.* calls don't count. Only functions the checker resolves are known: a member of a Pinia store isn't followed yet.

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