Skip to content

Advanced responses

Return a custom response from a nested Forms operation.

View Markdown

This is an Astro frontmatter excerpt for a page using the Forms setup. A nested operation throws to stop page processing immediately:

src/pages/account.astro
---
import { ThrowOverrideResponse } from '@astro-utils/forms/forms.js';
function requireUser() {
if (!Astro.locals.session.userId) {
throw new ThrowOverrideResponse(Astro.redirect('/login'));
}
}
requireUser();
---

Your authentication system must set session.userId after verifying the user. This guard is not an authentication implementation.

src/pages/export.astro
---
import { ThrowOverrideResponse } from '@astro-utils/forms/forms.js';
throw new ThrowOverrideResponse(new Response('name\nAlex\n', {
headers: { 'Content-Type': 'text/csv; charset=utf-8' }
}));
---
Argument Behavior
response Optional native Response (or null).
message Optional fallback message.

If no response is supplied, Forms uses Astro.locals.forms.overrideResponse. If that is also missing, the fallback message is returned with status 500. The default message is An error occurred, please try again later.

For an ordinary redirect from a button callback, use the redirect helper.