Skip to content
Cynosurev0.5.0
SearchStorybookGitHubnpmColour mode: System. Switch to Light

Components / Feedback and display

ErrorBoundary

Catches errors thrown while its children render and shows a fallback in their place, so one crashing part of the app leaves the rest working. The user can try again, and moving on (a new resetKey) recovers by itself.

Default

When to useLink to "When to use"

  • Around each part that can fail on its own: a tab's body, a side panel, a chart, a plugin's view.
  • Once around the whole app, as the last line, with a fallback that offers a reload.

Use something else when:

  • an operation failed (a request, a save): show it where it happened with a Notice or a Toast. Boundaries only catch errors thrown while rendering, not in event handlers or asynchronous code.
  • there is nothing to show yet: EmptyState.

UsageLink to "Usage"

tsx
Copy
import { ErrorBoundary } from "@arshad-shah/cynosure";

<ErrorBoundary label="Query results" resetKey={tabId} onError={(error, info) => report(error, info.componentStack)}>
  <ResultsPanel />
</ErrorBoundary>

Healthy children render as they are, with no wrapper element, so a boundary never changes the layout of a flex or grid parent.

Retry and resetLink to "Retry and reset"

The default fallback's "Try again" button renders the children again; if they still throw, the fallback comes back. A change of resetKey (compared with Object.is) does the same without the button: pass what the content depends on, such as the active tab or route.

Your own fallbackLink to "Your own fallback"

fallback gets the error and retry. Anything thrown that is not an Error (a string, say) arrives wrapped in one.

tsx
Copy
<ErrorBoundary
  fallback={(error, retry) => (
    <EmptyState title="The chart could not be drawn" description={error.message} actions={<Button onClick={retry}>Reload</Button>} />
  )}
>
  <Chart />
</ErrorBoundary>
Custom fallback

NestingLink to "Nesting"

The nearest boundary catches the error: an inner one around the editor keeps the window's sidebar and status bar on screen, and the outer one's onError is not called.

Nested boundaries catch locally

PropsLink to "Props"

Every prop of ErrorBoundary
PropWhat it doesTypeDefault
children
The part of the app this boundary protects. Rendered as it is, with no wrapper element.ReactNode
fallback
What to show instead of children after they threw while rendering: gets the error (anything thrown that is not an Error is wrapped in one) and retry, which renders the children again. Default: a danger Notice naming the part (label), with the error's message and a "Try again" button.((error: Error, retry: () => void) => ReactNode)
resetKey
When this value changes (compared with Object.is), a caught error is cleared and the children render again: pass what the content depends on (a tab or route id) so moving on recovers.unknown
onError
Called once per caught error, with React's component stack, for logging or reporting.((error: Error, info: ErrorInfo) => void)
label
Names the protected part in the default fallback's heading ("Query results could not be shown").string
labels
Replace the built-in English text of the default fallback: its heading and the retry button.Partial<ErrorBoundaryLabels>
className
Added to the default fallback's root, for layout.string
style
Layout only, on the default fallback's root. Restyle through --cy-error-boundary-* tokens.CSSProperties

Generated from the type definitions in @arshad-shah/cynosure 0.5.0.

Behaviour and accessibilityLink to "Behaviour and accessibility"

Behaviour and accessibility, table 1
InputResult
TabReaches "Try again" in the normal order; focus is not moved when the fallback appears
Enter / Space on "Try again"Renders the children again
  • Announcement: the default fallback is a danger Notice (role="alert"), so the failure is announced when it appears. Its heading names the part with label ("Query results could not be shown"; "Something went wrong" without one), and the error's message follows.
  • Reporting: onError(error, info) is called once per caught error, with React's component stack in info.componentStack.
  • Labels: labels replaces the English text: a title(label) function and the retry button's text.

TouchLink to "Touch"

The retry button is a small Button, with a 44px hit area on coarse pointers.

ThemingLink to "Theming"

Theming, table 2
TokenWhat it sets
--cy-error-boundary-paddingSpace around the default fallback, inside the area it replaces
--cy-error-boundary-max-widthWidest the default fallback's message grows

The message itself is a Notice: restyle it with --cy-notice-* tokens.

Notice, EmptyState, Toast

These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.

Getting started
Theming
Icons
Accessibility
Code editors and highlighters
Using Cynosure without React
Build-tool adapters
Components
Accordion
AppShell
AspectRatio
Breadcrumbs
Calendar
Card
Container
Disclosure
Divider
DocumentViewport
Grid
Inline
PageHeader
PanZoomCanvas
ScrollArea
SideNav
SortableList
SplitPane
Stack
StatusBar
Tabs
TitleBar
Toolbar
Tree
VirtualList
ZoomControls
Announcer
Avatar
Badge
CodeBlock
DescriptionList
EmptyState
ErrorBoundary
Field
Heading
IconTile
Image
Kbd
List
LoadingDots
Notice
ProgressBar
Prose
Skeleton
Stat
Steps
Table
Text
Button
Checkbox
Checklist
ColorField
ColorSwatches
Combobox
CompareSlider
CopyButton
DateField
FileDrop
IconButton
KeyValueEditor
Link
NumberField
Pagination
PathField
RadioGroup
SearchField
SegmentedControl
Select
SelectionFrame
SignaturePad
Slider
Switch
TagInput
TextArea
TextField
ThemeToggle
TimeField
ButtonBase
ChoiceBase
DialogBase
FileInputBase
HiddenValue
Icon
InputBase
LinkBase
LiveRegion
TextAreaBase
ChatMessage
Choice
DataGrid
DeviceFrame
HighlightedText
HotkeyField
ListRow
OptionList
OverlayFrame
QRCode
RowActions
Section
SiteFooter
SiteHeader
SkipLink
SortButton
VisuallyHidden
CommandPalette
ContextMenu
Dialog
FloatingToolbar
HoverCard
Menu
Menubar
Popover
ProgressOverlay
Sheet
Toast
Tooltip
Changelog