ErrorBoundary

The achilles heel of web apps seems to lie in error handling. An unhandled error in one component can bring your entire app down with it. Preferably, apps can be split into self-contained sections, where the failure of one component does not render the entire app unusable, only that one section.

Solid achieves this functionality with Error Boundaries. Error Boundary is a special component that ensures the unhandled errors of it's children don't extend beyond it's boundary.

tsx
import { ErrorBoundary } from "solid-start/error-boundary";
 
function Root() {
return (
<>
<ErrorBoundary>
<p>You can't see this text because of the error</p>
{() => {
throw new Error("Oh no! An error!");
}}
</ErrorBoundary>
<p>But this text is still here!</p>
</>
);
}
tsx
import { ErrorBoundary } from "solid-start/error-boundary";
 
function Root() {
return (
<>
<ErrorBoundary>
<p>You can't see this text because of the error</p>
{() => {
throw new Error("Oh no! An error!");
}}
</ErrorBoundary>
<p>But this text is still here!</p>
</>
);
}

Fallbacks

While a simple <ErrorBoundary /> component is enough to contain errors, You may notice that the error is very technical and could be a bit much for your users. You can provide a fallback prop to replace the default stack trace with your own code. Your fallback function takes a single parameter (the cause of the error, typically some derivative of the Error object), and returns an element.

tsx
<ErrorBoundary
fallback={(e: Error) => (
<>
<h2>Oh no! An Error!</h2>
<details>
<summary>Click here to learn more</summary>
<p>
<strong>{e.name}</strong>: {e.message}
</p>
</details>
</>
)}
>
<p>You can't see this text because of the error</p>
{() => {
throw new Error("Oh no! An error!");
}}
</ErrorBoundary>;
tsx
<ErrorBoundary
fallback={(e: Error) => (
<>
<h2>Oh no! An Error!</h2>
<details>
<summary>Click here to learn more</summary>
<p>
<strong>{e.name}</strong>: {e.message}
</p>
</details>
</>
)}
>
<p>You can't see this text because of the error</p>
{() => {
throw new Error("Oh no! An error!");
}}
</ErrorBoundary>;