Edge Function returned a non-2xx status code

On this page

Short answer

Your Edge Function answered with an error status, and supabase-js replaced the details with one generic sentence. The real status and response body are still on error.context. Log error.context.status and the body text, then fix what they point to: bad input, a crash in your function, a function that won't start, or a timeout.

Find a fix pack

Checklists and prompts for Supabase security, Stripe payments and deploys. Each pack is $24, or $49 for all three.

The exact error you'll see

supabase.functions.invoke() returns a FunctionsHttpError with this message:

Edge Function returned a non-2xx status code

Source: https://github.com/supabase/supabase-js/blob/master/packages/core/functions-js/src/types.ts (opens in a new tab)

The message never changes. Logging only error.message hides the useful part.

Applies to

  • Any app calling supabase.functions.invoke() from supabase-js
  • Lovable, Bolt and Cursor apps that call Supabase Edge Functions
  • Only FunctionsHttpError. supabase-js has separate error classes for a relay failure (FunctionsRelayError) and a failed fetch (FunctionsFetchError)

Why it happens

  • The function returned a status outside 200–299. In supabase-js, if (!response.ok) creates a FunctionsHttpError that keeps the response, and invoke() returns it with data: null.
  • Your code returned an error on purpose. Supabase tells you to "Return 400 for bad user input, 404 when something doesn't exist, 500 for server errors."
  • The function crashed. An uncaught exception ends as a 500.
  • The function didn't start. Supabase lists "Syntax errors preventing the function from loading" as a cause of a 503.
  • The function ran too long. A 504 Gateway Timeout. Supabase suggests "breaking large operations into smaller chunks."

How to fix it, step by step

  1. Find the failing invoke() call. Check the returned error, not only a surrounding try/catch, because invoke() returns the error instead of throwing it.
  2. Log the real status and body. This adapts Supabase's error-handling example to read the body as text, which works whatever format the function sent back:
import { FunctionsHttpError } from '@supabase/supabase-js'

const { data, error } = await supabase.functions.invoke('hello', {
  body: { foo: 'bar' },
})

if (error instanceof FunctionsHttpError) {
  console.log(error.context.status)
  console.log(await error.context.text())
}

If your function always returns JSON, Supabase's own example uses await error.context.json() instead. Read the body once: MDN says text() throws if the body "is disturbed or locked."

  1. Match the status to the cause:
    • 400: your function rejected the input. Fix what its message names.
    • 500: your function threw. Open its logs and fix that line.
    • 503: the function failed to start. Check for syntax and import errors, then redeploy.
    • 504: it ran too long. Speed up slow queries or outside API calls, or split the work.
    • 401: see Supabase Edge Function 401 (verify JWT). For a browser CORS error, see Supabase Edge Function CORS error.
  2. Open the function's logs in the Supabase dashboard. They capture console.log, console.warn and console.error output and uncaught exceptions. Add a log line around the step you suspect, reproduce once, and read it.
  3. Return clear errors from your function. Supabase recommends the right status code for each case because it "lets client apps handle different error types appropriately."
  4. Redeploy the function and repeat the exact request that failed.

What the docs don't spell out

  • json() can hide the real error. If the function sent back HTML or plain text, error.context.json() throws a SyntaxError because the body "cannot be parsed as JSON." Start with text(). (Response.json() (opens in a new tab))
  • The dashboard won't show you the body later. Supabase: "Request and response bodies are not logged or retained." Capture the body in your client log while you reproduce, and don't log secrets. (Edge Function logging (opens in a new tab))

How to check it worked

  • The same invoke() call returns data and error is null.
  • A deliberately bad request still returns a clear status and message you can show the user.
  • The function's logs show no new exception for that request.

Sources

  1. https://github.com/supabase/supabase-js/blob/master/packages/core/functions-js/src/types.ts (opens in a new tab): exact message and the FunctionsHttpError, FunctionsRelayError and FunctionsFetchError classes
  2. https://github.com/supabase/supabase-js/blob/master/packages/core/functions-js/src/FunctionsClient.ts (opens in a new tab): !response.ok check, error returned with data: null
  3. https://supabase.com/docs/guides/functions/error-handling (opens in a new tab): client example with error.context.json(), status code guidance
  4. https://supabase.com/docs/guides/functions/status-codes (opens in a new tab): 500, 503 and 504 causes, splitting large operations
  5. https://supabase.com/docs/guides/functions/logging (opens in a new tab): what logs capture, bodies not retained
  6. https://developer.mozilla.org/en-US/docs/Web/API/Response/text (opens in a new tab): text() and the disturbed or locked body error
  7. https://developer.mozilla.org/en-US/docs/Web/API/Response/json (opens in a new tab): json() throws SyntaxError on non-JSON

Tool to consider: Supabase.

Find a fix pack

Checklists and prompts for Supabase security, Stripe payments and deploys. Each pack is $24, or $49 for all three.