Errors in handlers
All core error rules apply inside .tsx components and JSX expressions—JSON.parse inside { } is flagged as unhandled-failure. Event handlers need extra care because React runs them asynchronously outside render (where Error Boundaries cannot catch them) and discards their return values. (For data fetching, see Async state.)
In short
- Handle failures inside the handler: Error Boundaries do not catch errors thrown from
onClickor other event callbacks. - Never drop a
Result: handleisErr(...)inside the handler and transition a single component stateEnum. - Render every outcome explicitly: use
<Match>soEditing,Rejected, andPublishedstay exhaustive.
Event handlers run on their own
React calls a handler when the event happens, long after the render that made the button has finished. An error boundary doesn't catch it either: boundaries only catch errors thrown while rendering. What escapes ends up in the console, and the user sees a button that does nothing. So a PropagateError must not leave a handler (uncaught-propagation):
✕ Rejecteduncaught-propagation
tsx
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class NotFound extends PropagateError {}
type Item = { readonly id: string; readonly name: string };
function find(items: readonly Item[], id: string): Item {
const item = items.find((i) => i.id === id);
if (item === undefined) throw new NotFound(id);
return item;
}
export function Items({ items }: { items: readonly Item[] }) {
return items.map((item) => (
<button key={item.id} onClick={() => console.log(`opening ${find(items, item.id).name}`)}>
{item.name}
</button>
));
}✅ Accepted: a missing item is a state of the screen, and <Match> shows each state
✓ Accepted0 diagnostics
tsx
import { useState } from "react";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { Match } from "@evobug_com/safe-ts-react/match";
type Item = { readonly id: string; readonly name: string };
type Opening = Enum<{ Browsing: {}; Opened: { item: Item }; Gone: {} }>;
const Opening = constructors<Opening>();
export function Items({ items }: { items: readonly Item[] }) {
const [opening, setOpening] = useState<Opening>(Opening.Browsing());
const open = (id: string): void => {
const item = items.find((i) => i.id === id);
setOpening(item === undefined ? Opening.Gone() : Opening.Opened({ item }));
};
return (
<>
{items.map((item) => (
<button key={item.id} onClick={() => open(item.id)}>{item.name}</button>
))}
<Match
on={opening}
Browsing={() => <p>Pick an item</p>}
Opened={({ item }) => <p>Opening {item.name}</p>}
Gone={() => <p>That item is gone</p>}
/>
</>
);
}tsx
import { useState } from "react";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { Match } from "@evobug_com/safe-ts-react/match";
type Item = { readonly id: string; readonly name: string };
type Opening = Enum<{ Browsing: {}; Opened: { item: Item }; Gone: {} }>;
const Opening = constructors<Opening>();
export function Items({ items }: { items: readonly Item[] }) {
const [opening, setOpening] = useState<Opening>(({ tag: "Browsing" } satisfies Opening as Opening));
const open = (id: string): void => {
const item = items.find((i) => i.id === id);
setOpening(item === undefined ? ({ tag: "Gone" } satisfies Opening as Opening) : ({ tag: "Opened", item } satisfies Opening as Opening));
};
return (
<>
{items.map((item) => (
<button key={item.id} onClick={() => open(item.id)}>{item.name}</button>
))}
<Match
on={opening}
Browsing={() => <p>Pick an item</p>}
Opened={({ item }) => <p>Opening {item.name}</p>}
Gone={() => <p>That item is gone</p>}
/>
</>
);
}✅ Also accepted: when find is shared code that throws a PropagateError for callers that end a whole request, the handler is the top for this one, and catches it
✓ Accepted0 diagnostics
tsx
import { useState } from "react";
import { PropagateError } from "@evobug_com/safe-ts-std/result";
class NotFound extends PropagateError {}
type Item = { readonly id: string; readonly name: string };
function find(items: readonly Item[], id: string): Item {
const item = items.find((i) => i.id === id);
if (item === undefined) throw new NotFound(id);
return item;
}
export function Items({ items }: { items: readonly Item[] }) {
const [message, setMessage] = useState("");
const open = (id: string): void => {
try {
setMessage(`opening ${find(items, id).name}`);
} catch (error) {
if (!(error instanceof NotFound)) throw error;
setMessage("That item is gone");
}
};
return (
<>
{items.map((item) => (
<button key={item.id} onClick={() => open(item.id)}>{item.name}</button>
))}
<p>{message}</p>
</>
);
}An async handler that can reject is the same mistake: onClick={sync} with an async sync() that can reject is floating-rejection. Catch inside it.
A Result returned to React is dropped
React throws away whatever a handler returns, so a Result returned to onClick is never looked at (must-use-result). That holds for a function passed as it is, and for an arrow that returns the call:
✕ Rejectedmust-use-result
tsx
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
type ArchiveError = Enum<{ NotDraft: {}; Locked: { by: string } }>;
const ArchiveError = constructors<ArchiveError>();
function archive(): Result<void, ArchiveError> {
return err(ArchiveError.Locked({ by: "Ada" }));
}
export function Toolbar() {
return (
<>
<button onClick={archive}>Archive</button>
<button onClick={() => archive()}>Archive</button>
</>
);
}tsx
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, type Result } from "@evobug_com/safe-ts-std/result";
type ArchiveError = Enum<{ NotDraft: {}; Locked: { by: string } }>;
const ArchiveError = constructors<ArchiveError>();
function archive(): Result<void, ArchiveError> {
return err(({ tag: "Locked", by: "Ada" } satisfies ArchiveError as ArchiveError));
}
export function Toolbar() {
return (
<>
<button onClick={archive}>Archive</button>
<button onClick={() => archive()}>Archive</button>
</>
);
}✅ Accepted: the component's state is one Enum of where archiving stands, and <Match> shows each state
✓ Accepted0 diagnostics
tsx
import { useState } from "react";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
import { Match } from "@evobug_com/safe-ts-react/match";
type ArchiveError = Enum<{ NotDraft: {}; Locked: { by: string } }>;
const ArchiveError = constructors<ArchiveError>();
type Archiving = Enum<{ Ready: {}; Refused: { error: ArchiveError }; Archived: {} }>;
const Archiving = constructors<Archiving>();
function archive(): Result<void, ArchiveError> {
return err(ArchiveError.Locked({ by: "Ada" }));
}
export function Toolbar() {
const [state, setState] = useState<Archiving>(Archiving.Ready());
const onArchive = (): void => {
const archived = archive();
setState(isErr(archived) ? Archiving.Refused({ error: archived.error }) : Archiving.Archived());
};
const button = <button onClick={onArchive}>Archive</button>;
return (
<Match
on={state}
Ready={() => button}
Refused={({ error }) => (
<>
{button}
<Match
on={error}
NotDraft={() => <p>Only drafts can be archived.</p>}
Locked={({ by }) => <p>{by} is editing it.</p>}
/>
</>
)}
Archived={() => <p>Archived.</p>}
/>
);
}tsx
import { useState } from "react";
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";
import { err, isErr, type Result } from "@evobug_com/safe-ts-std/result";
import { Match } from "@evobug_com/safe-ts-react/match";
type ArchiveError = Enum<{ NotDraft: {}; Locked: { by: string } }>;
const ArchiveError = constructors<ArchiveError>();
type Archiving = Enum<{ Ready: {}; Refused: { error: ArchiveError }; Archived: {} }>;
const Archiving = constructors<Archiving>();
function archive(): Result<void, ArchiveError> {
return err(({ tag: "Locked", by: "Ada" } satisfies ArchiveError as ArchiveError));
}
export function Toolbar() {
const [state, setState] = useState<Archiving>(({ tag: "Ready" } satisfies Archiving as Archiving));
const onArchive = (): void => {
const archived = archive();
setState(isErr(archived) ? ({ tag: "Refused", error: archived.error } satisfies Archiving as Archiving) : ({ tag: "Archived" } satisfies Archiving as Archiving));
};
const button = <button onClick={onArchive}>Archive</button>;
return (
<Match
on={state}
Ready={() => button}
Refused={({ error }) => (
<>
{button}
<Match
on={error}
NotDraft={() => <p>Only drafts can be archived.</p>}
Locked={({ by }) => <p>{by} is editing it.</p>}
/>
</>
)}
Archived={() => <p>Archived.</p>}
/>
);
}The state names each place the toolbar can be in: Ready before the first try, Refused with the reason, Archived after it worked. An Option<ArchiveError> would hold the error too, but its undefined would mean both "not tried yet" and "archived", so success would leave no trace. The error is a named Enum, not a string: each kind can carry its data (by), and the words the user reads are written once, in the inner <Match>.
When the error really doesn't matter, write onClick={() => void archive()}.
On a component (<Form onSubmit={save}>), the function is still a handler that runs later, so uncaught-propagation and floating-rejection apply. must-use-result doesn't: the component may use what it returns.
What the checker can't see: a component's own render errors reaching an error boundary. <Profile /> is not a call the checker follows, so a PropagateError thrown while rendering isn't traced to a boundary; don't throw during render.
Rules on this page: uncaught-propagation, floating-rejection, must-use-result