The application handles errors in two categories:
- Expected Errors
- Unexpected Errors
Expected errors are handled using the option-t library's Result type in server functions, which return Result<T, Error>, and in actions by returning errors to the client using Conform's submission.reply(). These include:
- Form validation errors
- Entity constraints (e.g., uniqueness violations)
- Not found errors
option-t example: add category function
conform example: add category action
Unexpected errors are thrown and caught by ErrorBoundary. These include:
- Database connection errors
- Network errors
- Runtime errors
- Any other unhandled errors
Example:
export async function loader() {
try {
const data = await getData();
return { data };
} catch (error) {
throw new Error("Unexpected error occurred", { cause: error });
}
}
export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
return (
<div>
<h1>Error</h1>
<p>An unexpected error occurred. Please try again later.</p>
</div>
);
}React component errors are caught by the nearest ErrorBoundary