"use client"; import { Component, type ReactNode } from "react"; // WHAT A STREAMED SECTION SHOWS WHEN ITS READ THROWS. // // A page that streams sends its shell first and each slow section when its // read is done (StreamSkeleton.tsx). A section's read can fail on its own — a // session file that cannot be parsed, a probe that throws — and without a // boundary of its own the failure climbs to the route's error page and takes // the shell with it: on /settings, the form. With one, the rest of the page // stays and the section's place says, in a sentence, that it could not be // loaded, and why. // // NO RETRY. Nothing here asks for the section again. The next render of the // page (a refresh, a navigation) hands this boundary new children, and it // shows whatever that render produced — the section, or the error again. // // The message is the error's own. In a production build React replaces a // server component's message with a generic one and a digest; the digest is // shown, because that is what the server's log names the real error by. type Props = { // data-stream-error="": the section's name, as its skeleton has it. name: string; // The sentence's subject: "The job list". what: string; // A section whose failure another boundary on the page already reports // (two sections of one read): hold nothing, say nothing. quiet?: boolean; children: ReactNode; }; type State = { error: (Error & { digest?: string }) | null }; export class StreamErrorBoundary extends Component { state: State = { error: null }; static getDerivedStateFromError(error: Error): State { return { error }; } componentDidUpdate(prev: Props) { // A new render of the page: show what it produced. if (this.state.error && prev.children !== this.props.children) { this.setState({ error: null }); } } render() { const { error } = this.state; if (!error) return this.props.children; if (this.props.quiet) return null; return (

{this.props.what} could not be loaded.{" "} {error.message} {error.digest ? ` (${error.digest})` : ""}

); } }