How to return an error from an edge function

I'm trying to handle errors from edge functions correctly on the client.

The docs show to return:
return new Response(err.message, { status: 400 })
https://github.com/supabase/examples/blob/main/supabase-js-v1/edge-functions/supabase/functions/stripe-webhooks/index.ts

Basic edge function:
  const { result } = await request.json();
  console.log(`Testing errors. Result requested: ${result}`)

  if (result === "success") {
    console.log('sending success')
    return new Response(JSON.stringify("You got a success"), {
      headers: { "Content-Type": "application/json" },
    });
  } else {
    console.log('sending error')
    return new Response("You got an error!", { status: 400, statusText: "Error" });
  }


Client:
  const {data, error} = await supabase.functions.invoke("test-errors", {
    body: JSON.stringify({ result: "error" }),
  });
  console.log('Test Result: ', data, error);


Client result: Test Result: {} null
Expected result: error to be not null
unknown.png
GitHub
Supabase examples to help you get started. Contribute to supabase/examples development by creating an account on GitHub.
Was this page helpful?