no-swallowed-error
A failure turned into undefined: if (isErr(result)) return undefined;, isErr(result) ? undefined : value, catch { return undefined; }, unwrapOr(result, undefined) (or null), and a rejection handler that does nothing: .catch(() => undefined), .catch(() => {}), .then(ok, () => undefined). An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.
Why: the caller gets an undefined that reads as "missing". That something failed, and why, is gone: a corrupt save file looks like no save file, an unreachable server like an empty list. Return the Result instead and let the caller decide; Result<Option<T>, E> keeps "missing" and "failed" apart. Guide: Errors.
✕ Rejectedno-swallowed-error
ts
import { parseJson } from "@evobug_com/safe-ts-std/json";
import { isErr, type Option } from "@evobug_com/safe-ts-std/result";
type Save = { readonly level: number };
export function savedLevel(text: string): Option<number> {
const save = parseJson<Save>(text);
if (isErr(save)) return undefined;
return save.level;
}ts
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 { parseJson } from "@evobug_com/safe-ts-std/json";
import { isErr, type Option } from "@evobug_com/safe-ts-std/result";
type Save = { readonly level: number };
export function savedLevel(text: string): Option<number> {
const save = __jsonParse<Save>(text, __v0);
if (isErr(save)) return undefined;
return save.level;
}
// ---- generated by safe-ts: JSON validators ----
type __T0 = { "level": number; };
function __v0(v: unknown, p: string): __JsonResult<__T0> {
if (!__jsonIsObject(v)) return __jsonFail(p, "object", v);
const __j1 = v["level"];
if (typeof __j1 !== "number") return __jsonFail(p + ".level", "number", __j1);
const __j2 = __j1;
const out: { "level": number; } = { "level": __j2 };
return out;
}✅ Accepted: the caller sees the error
✓ Accepted0 diagnostics
ts
import { type JsonResult, parseJson } from "@evobug_com/safe-ts-std/json";
import { q } from "@evobug_com/safe-ts-std/result";
type Save = { readonly level: number };
export function savedLevel(text: string): JsonResult<number> {
const save = q(parseJson<Save>(text));
return save.level;
}ts
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 JsonResult, parseJson } from "@evobug_com/safe-ts-std/json";
import { Err as __qErr } from "@evobug_com/safe-ts-std/result"; import { q } from "@evobug_com/safe-ts-std/result";
type Save = { readonly level: number };
export function savedLevel(text: string): JsonResult<number> {
const __q1 = __jsonParse<Save>(text, __v0); if (__q1 instanceof __qErr) return __q1; const save = __q1;
return save.level;
}
// ---- generated by safe-ts: JSON validators ----
type __T0 = { "level": number; };
function __v0(v: unknown, p: string): __JsonResult<__T0> {
if (!__jsonIsObject(v)) return __jsonFail(p, "object", v);
const __j1 = v["level"];
if (typeof __j1 !== "number") return __jsonFail(p + ".level", "number", __j1);
const __j2 = __j1;
const out: { "level": number; } = { "level": __j2 };
return out;
}Where a failure really does mean "missing" for this caller, deal with the error before dropping it. A branch that does anything else first (logs it, records it, shows it) isn't reported, and so isn't a fallback the caller can tell apart from a real value (isErr(save) ? 1 : save.level):
✅ Accepted: the error is reported before it's dropped
✓ Accepted0 diagnostics
ts
import { parseJson } from "@evobug_com/safe-ts-std/json";
import { isErr, type Option } from "@evobug_com/safe-ts-std/result";
type Save = { readonly level: number };
export function savedLevel(text: string, report: (problem: string) => void): Option<number> {
const save = parseJson<Save>(text);
if (isErr(save)) {
report(`save ignored: ${save.error.message}`);
return undefined;
}
return save.level;
}ts
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 { parseJson } from "@evobug_com/safe-ts-std/json";
import { isErr, type Option } from "@evobug_com/safe-ts-std/result";
type Save = { readonly level: number };
export function savedLevel(text: string, report: (problem: string) => void): Option<number> {
const save = __jsonParse<Save>(text, __v0);
if (isErr(save)) {
report(`save ignored: ${save.error.message}`);
return undefined;
}
return save.level;
}
// ---- generated by safe-ts: JSON validators ----
type __T0 = { "level": number; };
function __v0(v: unknown, p: string): __JsonResult<__T0> {
if (!__jsonIsObject(v)) return __jsonFail(p, "object", v);
const __j1 = v["level"];
if (typeof __j1 !== "number") return __jsonFail(p + ".level", "number", __j1);
const __j2 = __j1;
const out: { "level": number; } = { "level": __j2 };
return out;
}if (isErr(a) || isErr(b)) return undefined; counts too. No quick fix: the return type changes, and so do the callers.
A one-line helper that does one of these to its parameter is followed: its calls are reported too, in any file that imports it. Naming the swallow doesn't make it a decision:
✕ Rejectedno-swallowed-error
ts
import { parseJson } from "@evobug_com/safe-ts-std/json";
import { type Option, type Result, unwrapOr } from "@evobug_com/safe-ts-std/result";
export const orNone = <T, E>(result: Result<Option<T>, E>): Option<T> => unwrapOr(result, undefined);
type Draft = { readonly text: string };
export function restoredDraft(stored: string): Option<Draft> {
return orNone(parseJson<Draft>(stored));
}ts
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 { parseJson } from "@evobug_com/safe-ts-std/json";
import { type Option, type Result, unwrapOr } from "@evobug_com/safe-ts-std/result";
export const orNone = <T, E>(result: Result<Option<T>, E>): Option<T> => unwrapOr(result, undefined);
type Draft = { readonly text: string };
export function restoredDraft(stored: string): Option<Draft> {
return orNone(__jsonParse<Draft>(stored, __v0));
}
// ---- generated by safe-ts: JSON validators ----
type __T0 = { "text": string; };
function __v0(v: unknown, p: string): __JsonResult<__T0> {
if (!__jsonIsObject(v)) return __jsonFail(p, "object", v);
const __j1 = v["text"];
if (typeof __j1 !== "string") return __jsonFail(p + ".text", "string", __j1);
const __j2 = __j1;
const out: { "text": string; } = { "text": __j2 };
return out;
}A handler passed by a name nothing else reads (const ignore = () => undefined; load().catch(ignore)) is read as if written there.
A fallback the caller can tell apart from a value (unwrapOr(result, 0), .catch(() => [])) isn't reported; whether 0 or an empty list is a fair answer for a failure is the caller's call, made in the open. A .catch(() => {}) kept from when a function rejected and that now returns a Result is must-use-result, with or without this rule.