Form Errors
Both standard HTML forms and progressively enhanced forms can handle errors gracefully. With {@attach enhance(...)}, the error message is shown inline. Without it, Mochi renders the server error page.
The action throws a plain Error. With {@attach enhance(...)} the error message is shown inline. Without it, the server renders the Mochi error page.
With {@attach enhance(...)}
The enhance attachment catches the thrown error and shows it inline
Plain HTML
Plain POST, server throws, Mochi renders the error page (press back to return)
<script lang="ts">
import ErrorDemo from './ErrorDemo.svelte';
import { files } from './files.ts';
const sources = await loadSources(files);
</script>
<p>
The action throws a plain <code>Error</code>. With <code>{'{@attach enhance(...)}'}</code> the error message is shown inline. Without it, the server renders the Mochi error page.
</p>
<h3>With <code>{'{@attach enhance(...)}'}</code></h3>
<ErrorDemo label="The enhance attachment catches the thrown error and shows it inline" mochi:hydrate />
<h3>Plain HTML</h3>
<ErrorDemo label="Plain POST, server throws, Mochi renders the error page (press back to return)" />
Styles
<style>
h3 {
margin: 1.5rem 0 0.25rem;
font-size: 0.95rem;
font-weight: 600;
color: var(--text-muted);
}
p {
margin: 0 0 0.5rem;
font-size: 0.9rem;
color: var(--text-muted);
}
code {
background: var(--code-bg);
color: var(--code-accent);
padding: 0.05rem 0.35rem;
border-radius: var(--radius-sm);
font-family: var(--font-mono);
font-size: 0.85rem;
}
</style>
More demos
Data Loading Server-side fetch from PokéAPI cached via MochiCache and rendered at request time. Error Handling Catch render errors and unmatched routes via Mochi.serve()’s errorPage option and the handleError hook. File Uploads via form actions multipart/form-data submission, validated with fail() and success(), shown enhanced and plain.