{"record":{"id":"b0c0659601b45565","repo":"sveltejs/kit","slug":"cannot-return-error-use-error-or","errorCode":null,"errorMessage":"Cannot `return error(...)` — use `error(...)` or `return fail(...)` instead","messagePattern":"Cannot `return error\\(\\.\\.\\.\\)` — use `error\\(\\.\\.\\.\\)` or `return fail\\(\\.\\.\\.\\)` instead","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/kit/src/runtime/server/page/actions.js","lineNumber":283,"sourceCode":"\t\t\t\tcurrent.setAttributes({\n\t\t\t\t\t'sveltekit.form_action.result.type': 'failure',\n\t\t\t\t\t'sveltekit.form_action.result.status': result.status\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn result;\n\t\t}\n\t});\n}\n\n/** @param {any} data */\nfunction validate_action_return(data) {\n\tif (data instanceof Redirect) {\n\t\tthrow new Error('Cannot `return redirect(...)` — use `redirect(...)` instead');\n\t}\n\n\tif (data instanceof HttpError) {\n\t\tthrow new Error('Cannot `return error(...)` — use `error(...)` or `return fail(...)` instead');\n\t}\n}\n\n/**\n * Try to `devalue.uneval` the data object, and if it fails, return a proper Error with context\n * @param {any} data\n * @param {string} route_id\n */\nexport function uneval_action_response(data, route_id) {\n\treturn try_serialize(data, uneval, route_id);\n}\n\n/**\n * @param {any} data\n * @param {(data: any) => string} fn\n * @param {string} route_id\n */\nfunction try_serialize(data, fn, route_id) {","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/runtime/server/page/actions.js#L265-L301","documentation":"`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)`.","triggerScenarios":"Writing `return error(400, 'bad input')` inside a form action instead of `error(400, 'bad input')` thrown, or `return fail(400, {...})`.","commonSituations":"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).","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')`"],"exampleFix":"// before\nexport const actions = {\n  default: async ({ request }) => {\n    const data = await request.formData();\n    return error(400, 'invalid');\n  }\n};\n// after\nimport { fail } from '@sveltejs/kit';\nexport const actions = {\n  default: async ({ request }) => {\n    const data = await request.formData();\n    return fail(400, { message: 'invalid' });\n  }\n};","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["sveltekit","error-handling","form-actions","api-misuse"],"backgroundTag":"error-helper-misuse","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}