Error handling
Swift Rust gives you three tools to handle errors: error.tsx boundaries for runtime errors, notFound() for missing data, and redirect() for control flow.
Error boundaries
Create an error.tsx file in any segment. The closest one in the tree catches the error and renders the fallback UI.
app/dashboard/error.tsx
"use client";
export default function ErrorBoundary({
error,
reset,
}: {
error: Error;
reset: () => void;
}) {
return (
<div>
<h2>Something went wrong</h2>
<pre>{error.message}</pre>
<button onClick={reset}>Try again</button>
</div>
);
}Throwing notFound()
From a page or layout, call notFound() to render the closest not-found.tsx. The function throws a special NotFoundError that the framework catches and converts to a 404 response.
app/blog/[slug]/page.tsx
import { notFound } from "swift-rust/router";
import { getPost } from "@/lib/posts";
export default async function PostPage({ params }: { params: { slug: string } }) {
const post = await getPost(params.slug);
if (!post) notFound();
return <article>{post.content}</article>;
}Redirects
Use redirect(url) to send the user to a different URL, or permanentRedirect(url) for a 308 (permanent) redirect.
usage
import { redirect, permanentRedirect } from "swift-rust/router";
if (!session) redirect("/login");
if (post.oldSlug) permanentRedirect(`/blog/${post.newSlug}`);Global error overlay
In development, the framework shows a Rust-styled error overlay with the source frame, the file and line number, and a button to open the file in your editor.