result-vs-exceptions

작성자: posthog

코드 리뷰 가이드: Result 타입과 예외 던지기 중 선택하기. 복구 가능하고 예상되는 실패는 Result 값으로 반환해야 하며, 오직...

npx skills add https://github.com/posthog/posthog-definitions --skill result-vs-exceptions

Result types vs. exceptions

A code-review lens for the question: should this failure be a Result<T, E> or a throw?

The convention in this repo: recoverable, expected failures return a Result. Unrecoverable bugs throw.

Why this matters

Exceptions are invisible in the type signature. A function that throws looks pure from the outside — the caller has no compiler-enforced reminder that failure is possible, no list of failure kinds, no exhaustiveness check. That's fine for "the runtime is broken" failures (out-of-memory, broken invariants, programmer mistakes), because no caller can sensibly recover and the crash is the right behavior.

It's the wrong default for failures that are part of normal control flow. Invalid user input, validation issues, parse errors, "not found" lookups, expected network outcomes (4xx, rate limits) — these are not exceptional. They are predictable branches of the API. Hiding them inside try/catch puts business logic in the catch block, lets callers forget the failure path entirely, and conflates "this might fail" with "this should never happen."

Result<T, E> flips both problems: failure is part of the return type, the caller is forced by the type system to discriminate, and the set of failure kinds is enumerable.

Decision

When reviewing a new throw or a new error class, ask:

  1. Is failure part of this function's normal contract? If the function is defined by "validate", "parse", "find", "fetch and decode", "match against a schema" — failure is part of the contract. Return Result. Examples in this repo: validate(state), loading definitions, decoding API responses.
  2. Would a reasonable caller want to do something other than crash? Branch on the error, retry, show a friendlier message, fall back to a default, accumulate issues across many calls. If yes — Result. If the only sensible reaction is "log and die," throw.
  3. Is the failure a broken invariant the program cannot reason past? "This map should contain X but doesn't", "this branch is unreachable", "the schema generator produced an impossible shape." These should throw — they are bugs, not data.
  4. Will the failure cross many call frames? Result types are explicit and verbose to thread; that explicitness is the feature for one or two layers, but a noise tax across ten. If a low-level invariant violation needs to escape to the top, throwing is reasonable — but only if no intermediate layer could meaningfully recover.

Patterns to flag

  • A new throw for input validation. Almost always wants to be a Result. The caller already knows invalid input is possible.
  • A try/catch that does if (err instanceof FooError) { … return 1; } throw err;. The control flow is "if expected error, handle; otherwise propagate." That's exactly what Result expresses without the wrapping.
  • throw new SomethingError("not found"). "Not found" is usually a value (undefined, null, Result.err("not-found")), not an exception. Flag unless the API explicitly promises the item exists.
  • An error class with no behavior — just a name and a string message. It exists only so callers can instanceof it. That's a sign the failure is part of normal flow and would be better modeled as a Result variant with a string tag.
  • Mixed signaling in the same path. A function that returns Result for one failure but throws for another similar-shaped failure. Pick one.
  • A Result whose error type is Error or unknown. Defeats the point. The whole value of Result<T, E> is that E is a tight, enumerable shape — "not-found" | "invalid-tag" | { kind: "rate-limited"; retryAfter: number } — so callers can exhaustively switch on it.

Patterns that are fine

  • Throwing inside bin/, CLI entry points, or top-level handlers when the program genuinely cannot continue (config missing, network completely unreachable on startup). The caller is the OS; the OS handles exit codes.
  • Throwing for "this branch is unreachable" defaults. throw new Error("unreachable") inside the default of an exhaustive switch is healthier than returning a fake value.
  • Throwing for I/O the function cannot meaningfully recover from (filesystem broken, OOM) — provided no intermediate frame would have wanted to catch it.
  • Returning T | undefined for binary "found / not found" cases. It's a degenerate Result and the convention here treats it as equivalent.

Shape

Use the project's Result<T, E> from src/result.ts:

import { type Result, ok, err } from "../result.js";

export function parseTag(raw: string): Result<Tag, "empty" | "missing-prefix"> {
  if (raw.length === 0) return err("empty");
  if (!raw.startsWith("iac:")) return err("missing-prefix");
  return ok({ raw });
}

Prefer narrow string-literal or tagged-union E shapes to opaque Error objects. The caller benefits from the type system telling them every failure case; downgrading to Error throws that away.

Reference

See src/apply/validate.ts for the canonical example in this repo: validate returns Result<ValidationOk, string[]> so the CLI can format the issues and exit with code 1 without a try/catch.

posthog의 다른 스킬

error-tracking-hono
posthog
PostHog 오류 추적 for Hono
tuning-incremental-sync-config
posthog
동기화의 구성은 ExternalDataSchema에 저장되며, external-data-schemas-partial-update를 통해 언제든지 변경할 수 있습니다. 대부분의 변경은 비파괴적이며(다음 동기화에 적용됨), 일부 변경(sync_type 전환, 기본 키 변경)은 동기화된 데이터 손상을 방지하기 위해 신중한 처리가 필요합니다.
playwright-test
posthog
플레이라이트 테스트를 작성하고, 실행이 잘 되며, 불안정하지 않도록 하세요.
error-tracking-ruby
posthog
PostHog Ruby 오류 추적
authoring-log-alerts
posthog
PostHog 프로젝트의 서비스에 유용하고 노이즈가 적은 로그 알림을 작성합니다. 사용자가 로그에 대한 알림 설정을 요청하거나 추가해야 할 알림을 제안할 때 사용하세요.
making-scenes-tab-aware
posthog
Guides converting PostHog frontend scenes to be tab aware for internal scene tabs. Use when adding or refactoring a `SceneExport` scene, fixing state leaking…
posthog-survey-creator
posthog
PostHog에서 안내 대화를 통해 설문조사를 생성하고 구성합니다. 사용자가 설문조사를 만들거나, 사용자 피드백을 수집하거나, 실행하려 할 때 이 스킬을 사용하세요.
authoring-scouts
posthog
PostHog Signals 스카우트를 작성, 편집 및 조정하는 방법 — 프로젝트를 스캔하고 Signals 인박스에 보고서를 작성하는 예약된 에이전트입니다. 사용자가…