如何排查 Supabase Edge Function 的非 2xx 錯誤(FunctionsHttpError)
Fix Supabase "Edge Function returned a non-2xx status code" (FunctionsHttpError)
這篇指南説明如何排查 Supabase Edge Function 回傳非 2xx 狀態碼時出現的 FunctionsHttpError,並從客戶端讀取真正的錯誤回應。
Your app has a button that calls a Supabase Edge Function: checkout, send an email, ask an AI model. You click it and the toast says only this:
FunctionsHttpError: Edge Function returned a non-2xx status code
That message isn't the error. It's supabase-js telling you the function answered with a status outside 200–299. The real reason is in the response your app never read, and in the function's logs. Below is how I find it, what each status code usually means, and how I change the function so the next failure explains itself.
It's based on Supabase's guides to Edge Function error handling, status codes, logging and secrets, its knowledge base articles on 401, 404 and 500 responses, and the supabase-js source, all read on 29 November 2026.
What the message actually means
The client code is short. In supabase-js, functions.invoke() checks response.ok, and when it's false it throws a FunctionsHttpError with the whole Response attached as error.context. You get data: null, and the body your function sent is still unread on that Response.
Supabase's error handling guide lists three error types, and they point to different places:
-
FunctionsHttpError: "Your function executed but returned an error (4xx/5xx status)". This is the one this post is about. -
FunctionsRelayError: a problem between the client and Supabase. -
FunctionsFetchError: "Function couldn't be reached at all". Its message isFailed to send a request to the Edge Function.
The fetch error can mislead you. The 404 article warns that browsers misreport platform 404s as CORS errors, so the client shows only that generic message. Check the logs before you assume the network is down.
Step 1: read the real error in the app
Supabase's docs use await error.context.json(). I read it as text first, because not every error body is JSON: the 500 article shows a plain Internal Server Error for an uncaught exception, and .json() would throw on that.
import { FunctionsHttpError } from '@supabase/supabase-js'
const { data, error } = await supabase.functions.invoke('create-checkout', {
body: { priceId },
})
if (error instanceof FunctionsHttpError) {
const status = error.context.status
const text = await error.context.text()
let details = text
try {
details = JSON.parse(text)
} catch {
// not JSON, keep the text
}
console.error('Edge Function failed', status, details)
}
Read the body once. It's a standard Response, and MDN's page on bodyUsed shows that reading it marks the body as used. If Lovable or another tool writes your code, paste this in where the call is made or ask the tool to add it. You now have two facts: a status code and a message.
Step 2: open the function's logs
In the Supabase Dashboard: Edge Functions, pick the function, then the Logs tab. The logging guide says it shows one row per request "with the status code and execution time", plus your console output, uncaught exceptions, and boot and shutdown events.
One thing to know: "Request and response bodies are not logged or retained." If you need to see what came in, log the part you need yourself, and leave out anything sensitive.
On Lovable's built-in backend, Lovable's docs put it at More → Database → Manage → Edge functions, then View logs on the function. Lovable also notes that new apps created from 13 May 2026, which use TanStack Start, often run their server code inside the app instead, while older React + Vite apps run it as edge functions.
Step 3: match the status code
Supabase's status codes page is the map. These are the ones I see most.
401. Either the platform rejected the request before your code ran, or your code returned 401. The 401 article tells them apart by the body: "Invalid Token" or "Missing authorization header" comes from the built-in JWT check; your own message, or an empty body, means your code ran. A common platform case: your project uses the new asymmetric keys, but "the legacy auth verification check only understands the legacy format." The fix is to turn off verify_jwt for that function and check the caller in code. Don't stop at the first half. Supabase's function configuration page warns that without the check "anyone" can invoke the function, so pair it with an auth check such as withSupabase({ auth: 'user' }) from the Securing Edge Functions guide.
404. The name in invoke('...') doesn't match a deployed function, or the bundle is missing. The 404 article separates NOT_FOUND ("Requested function was not found") from NOT_FOUND_FUNCTION_BLOB, which can happen "even when the function appears as ACTIVE", and both are fixed by checking the name and redeploying.
500. Your function threw an uncaught exception (WORKER_ERROR), or your code returned 500 on purpose. The log entry shows the error type and the file and line. In AI-built apps, the first thing I check is a secret the function reads with Deno.env.get() that was never set on this project; a value that's undefined turns into a TypeError a few lines later. The second is a third-party API failing, with the function passing that failure on. The 500 article also covers a CORS special case: without an OPTIONS handler, browser requests can log SyntaxError: Unexpected end of JSON input.
503, 504, 546. 503 is BOOT_ERROR: the function didn't start, usually a syntax or import error, and the page suggests running it locally with supabase functions serve. 504 means it didn't respond within the request timeout. 546 is WORKER_RESOURCE_LIMIT: the 546 article lists 2 seconds of CPU and 250MB of memory per isolate.
Step 4: make the function explain its failures
Most of these take longer than they should because the function returns a bare 500. Supabase's guide recommends 400 for bad input, 404 when something doesn't exist and 500 for server errors, with a helpful message in the body. This is the shape I use:
import { corsHeaders } from 'npm:@supabase/supabase-js@^2/cors'
Deno.serve(async (req) => {
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders })
}
try {
const stripeKey = Deno.env.get('STRIPE_SECRET_KEY')
if (!stripeKey) {
console.error('STRIPE_SECRET_KEY is not set on this project')
return Response.json(
{ error: 'Server configuration is incomplete' },
{ status: 500, headers: corsHeaders },
)
}
const { priceId } = await req.json()
if (!priceId) {
return Response.json(
{ error: 'priceId is required' },
{ status: 400, headers: corsHeaders },
)
}
// call the payment API here, then return what the app needs
return Response.json({ ok: true }, { headers: corsHeaders })
} catch (err) {
console.error('create-checkout failed:', err)
return Response.json(
{ error: 'Something went wrong' },
{ status: 500, headers: corsHeaders },
)
}
})
Three details matter. The CORS headers go on error responses too, as in Supabase's CORS guide example; otherwise the browser can block your app from reading the body. Importing corsHeaders from the SDK needs supabase-js v2.95.0 or later. And the detail goes to console.error, where only you can read it, while the response says what went wrong without echoing secrets or stack traces.
Step 5: set the secret where the function runs
Supabase's secrets guide puts it simply: "Local development and production load secrets differently, so set them in both." Locally, functions read supabase/functions/.env. In production, add the secret in the Dashboard under Edge Function Secrets, or from the CLI:
supabase secrets set STRIPE_SECRET_KEY=sk_live_...
supabase secrets list
Your functions read a new secret immediately, with no redeploy. Names can't start with SUPABASE_. On Lovable, secrets are injected into Edge Functions automatically, and anything prefixed VITE_ is a browser value that belongs in .env, not in Secrets.
Prove it works
- Trigger the action again and confirm a 2xx row in the function's logs.
- Send bad input on purpose and check that your app shows the 400 message, not the generic one.
- Sign out and call the function: you should get your own 401, not data.
If the browser console shows a CORS error next to the 2xx message, I wrote that fix up here: Fix CORS errors calling Supabase Edge Functions. And if your AI tool wrote a lot of Edge Functions and you'd like a second look at the code, the free AI app audit reports findings with the file and line.
I wrote this with AI assistance and reviewed it myself.
來源:DEV Community · dev.to