sveltejs/kit · error
Cannot `return error(...)` — use `error(...)` or `return fai
Error message
Cannot `return error(...)` — use `error(...)` or `return fail(...)` instead
What it means
`error(...)` creates an `HttpError` that must be thrown so SvelteKit can convert it into an error response. Returning the instance from an action is invalid; `validate_action_return` throws this Error telling you to either throw the error or, for form validation feedback, return `fail(status, data)`.
Source
Thrown at packages/kit/src/runtime/server/page/actions.js:283
current.setAttributes({
'sveltekit.form_action.result.type': 'failure',
'sveltekit.form_action.result.status': result.status
});
}
return result;
}
});
}
/** @param {any} data */
function validate_action_return(data) {
if (data instanceof Redirect) {
throw new Error('Cannot `return redirect(...)` — use `redirect(...)` instead');
}
if (data instanceof HttpError) {
throw new Error('Cannot `return error(...)` — use `error(...)` or `return fail(...)` instead');
}
}
/**
* Try to `devalue.uneval` the data object, and if it fails, return a proper Error with context
* @param {any} data
* @param {string} route_id
*/
export function uneval_action_response(data, route_id) {
return try_serialize(data, uneval, route_id);
}
/**
* @param {any} data
* @param {(data: any) => string} fn
* @param {string} route_id
*/
function try_serialize(data, fn, route_id) {View on GitHub (pinned to 03f1687fe6)
Solutions
- Use `return fail(status, data)` for validation failures in form actions
- Use bare `error(status, message)` (thrown) for fatal errors: `throw error(404, 'Not found')`
Example fix
// before
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
return error(400, 'invalid');
}
};
// after
import { fail } from '@sveltejs/kit';
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
return fail(400, { message: 'invalid' });
}
}; Defensive patterns
Strategy: validation
Prevention
- Rule of thumb: user input problems -> `return fail(...)`; unexpected fatal problems -> `throw error(...)`
- Never prefix error/redirect with `return` in actions or load functions
- Lint for `return error(` and `return redirect(` in the codebase
When it happens
Trigger: Writing `return error(400, 'bad input')` inside a form action instead of `error(400, 'bad input')` thrown, or `return fail(400, {...})`.
Common situations: Confusing `fail()` (expected failure data for forms) with `error()` (fatal error response); copying pre-SvelteKit-2 patterns where `error` was used differently; mixing up return-based APIs from other frameworks (Next.js, Remix).
Related errors
- Cannot `return redirect(...)` — use `redirect(...)` instead
- HTTP error status codes must be between 400 and 599 — ${stat
- Error: ${status}
- Data returned from action inside ${route_id} is not serializ
- Data returned from action inside ${route_id} is not serializ
AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02).
Data as JSON: /api/errors/b0c0659601b45565.
Report an issue: GitHub.