# Error handling calling Edge Functions - Supabase

**URL:** <https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634>\
**Category:** Ask us anything\
**Tags:** supabase\
**Created:** [September 4, 2024, 9:36am UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634 "2024-09-04T09:36:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philibert](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Philibert](https://community.weweb.io/u/Philibert)\
**Post date:** [September 4, 2024, 9:36am UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/1 "2024-09-04T09:36:04Z")

</div>

Hey ! I have an issue with calling supabase edge functions.  
In my Edge Function, I raise 409 erros if conditions are not respected, with a message explainig the error (eg : {success: false, message: “condition 1 not validated”}).

However, weweb always raises a “non-2xx” error, saying it’s CORS issue, while debugging from google’s inspector receives the right error message.  
It appears to me that Weweb automatically raises this CORS error if the response is not “2xx”

I thought of a solution : raising only 200 even for errors (i do not like that)

Any one had the same issue ? Found a fix ?

Thanks !

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [September 4, 2024, 9:48am UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/2 "2024-09-04T09:48:03Z")

</div>

I’d say maybe just throw simple Code 400 errors and it should work. Works for my functions at least

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [September 4, 2024, 1:54pm UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/3 "2024-09-04T13:54:30Z")

</div>

in your edge function do you handle CORS with the 4xx responses or only with the 2xx?

---

<div class="post-metadata">

**Author:** ![Philibert](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Philibert](https://community.weweb.io/u/Philibert)\
**Post date:** [September 4, 2024, 2:52pm UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/4 "2024-09-04T14:52:06Z")

</div>

Hey ! Thank you for answering. I tried going from 409 to 400 but I still have the same issue … Message is fine in the response in the chrome dev tool but in weweb “non-2xx”  
I tried using 200 and the message displays good in weweb … but then the errors are not raised …

---

<div class="post-metadata">

**Author:** ![Philibert](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Philibert](https://community.weweb.io/u/Philibert)\
**Post date:** [September 4, 2024, 2:53pm UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/5 "2024-09-04T14:53:54Z")

</div>

Hey ! thanks for answering  
In ly CORS settings i do not specify the types of errors. Here is my CORS file settings :

export const corsHeaders = {  
‘Access-Control-Allow-Origin’: ‘\*’,  
‘Access-Control-Allow-Headers’: ‘authorization, x-client-info, apikey, content-type’,  
‘Access-Control-Allow-Methods’: ‘POST, GET, OPTIONS’,  
}

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [September 4, 2024, 2:56pm UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/6 "2024-09-04T14:56:25Z")

</div>

Are you sending the CORS headers with the response? Check out the `headers: corsHeaders` here you can see me sending both 400 and 405

 ![Screenshot 2024-09-04 at 16.56.07](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/f/fc864d0be85b1a5a548d997eebb81e4dfd96b749.png)

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [September 4, 2024, 3:19pm UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/7 "2024-09-04T15:19:08Z")

</div>

CORS errors are not specified by you or weweb. they are handled by the browser. you just need to check that your backend includes the right headers in every respone, included when replying with an error code.

---

<div class="post-metadata">

**Author:** ![Philibert](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Philibert](https://community.weweb.io/u/Philibert)\
**Post date:** [September 5, 2024, 5:43am UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/8 "2024-09-05T05:43:24Z")

</div>

Ok thank you so much for your answers guys ! I will dig into that 😉

---

<div class="post-metadata">

**Author:** ![Philibert](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Philibert](https://community.weweb.io/u/Philibert)\
**Post date:** [September 8, 2024, 11:07am UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/9 "2024-09-08T11:07:36Z")

</div>

> [@dorilama](#):
>
> specified by you or weweb. they are handled by the browser. you just need to check that your backend includes the right head

Hey Guys !  
I am still struggling with these CORS Issues  
In my backend i always include the CORS headers, exactly like specified in the supabase doc [CORS (Cross-Origin Resource Sharing) support for Invoking from the browser | Supabase Docs](https://supabase.com/docs/guides/functions/cors)  
I still have the good errors in my browser ! So i really thonk the problem comes from weweb … @Joyce  
Here are the logs from weweb when the error occurs :

WeWeb :

````auto
name: "Error"
stack: "Error: Edge Function returned a non-2xx status code at Object.invokeEdgeFunction (https://cdn.weweb.io/components/f9ef41c3-1c53-4857-855b-f2f6a40b7186/c879c29f-d9c5-478f-be74-f00db1e91f48/dist/manager.js:1:235188) at async le (https://editor-cdn.weweb.io/ww_front/public/js/index.31dd0cb8.js:1:427205) at async ae (https://editor-cdn.weweb.io/ww_front/public/js/index.31dd0cb8.js:1:412070) at async ae (https://editor-cdn.weweb.io/ww_front/public/js/index.31dd0cb8.js:1:412461) at async ae (https://editor-cdn.weweb.io/ww_front/public/js/index.31dd0cb8.js:1:412461) at async ae (https://editor-cdn.weweb.io/ww_front/public/js/index.31dd0cb8.js:1:412461) at async ae (https://editor-cdn.weweb.io/ww_front/public/js/index.31dd0cb8.js:1:412461) at async ae (https://editor-cdn.weweb.io/ww_front/public/js/index.31dd0cb8.js:1:412461) at async ae (https://editor-cdn.weweb.io/ww_front/public/js/index.31dd0cb8.js:1:412461) at async ae (https://editor-cdn.weweb.io/ww_front/public/js/index.31dd0cb8.js:1:412461)"
message: "Edge Function returned a non-2xx status code"
cause
name: "FunctionsHttpError"
context
type: "cors"
url: "https://htdvnllbwwrxuawiydlz.supabase.co/functions/v1/song-request"
redirected: false
status: 400
ok: false
statusText: ""
bodyUsed: false
arrayBuffer: function arrayBuffer() { [native code] }
blob: function blob() { [native code] }
clone: function clone() { [native code] }
formData: function formData() { [native code] }
json: function json() { [native code] }
text: function text() { [native code] }
headers
append: function append() { [native code] }
delete: function delete() { [native code] }
get: function get() { [native code] }
getSetCookie: function getSetCookie() { [native code] }
has: function has() { [native code] }
set: function set() { [native code] }
entries: function entries() { [native code] }
forEach: function forEach() { [native code] }
keys: function keys() { [native code] }
values: function values() { [native code] }
body
locked: false
cancel: function cancel() { [native code] }
getReader: function getReader() { [native code] }
pipeThrough: function pipeThrough() { [native code] }
pipeTo: function pipeTo() { [native code] }
tee: function tee() { [native code] }
values: function values() { [native code] }```
````

---

<div class="post-metadata">

**Author:** ![dorilama](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/dorilama/32/10403_2.png) [@dorilama](https://community.weweb.io/u/dorilama)\
**Post date:** [September 8, 2024, 4:26pm UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/10 "2024-09-08T16:26:29Z")

</div>

all right. it is actually how the supabase sdk works: it will throw an error if the function returns a non 2xx response.

if you correctly add CORS headers in the non 2xx response as well and you are returning data with it you can access it like this:

- go to the “on error” part of the workflow that invokes the edge function  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/8/8aaea0878826969cfa5feae8bf8729baf427af88.png)

- check that the current error is actually this specific error returned by the sdk by checking `context.workflow.error?.['cause']?.['name']=='FunctionsHttpError'`

![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/9/95915b585e8f171d43d62b3e700426f9003aeb08.png)

 ![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/b/bff19b857bb58c1a7172a2049bdec50043b52566.png)

- the data in the response is accessible in the actions like this `await context.workflow.error.cause.context.json();`

![image](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/d/d06e20073c50a0651898f4e8f87ac6a657a4a1cc.png)

---

<div class="post-metadata">

**Author:** ![Philibert](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Philibert](https://community.weweb.io/u/Philibert)\
**Post date:** [September 9, 2024, 8:32am UTC](https://community.weweb.io/t/error-handling-calling-edge-functions-supabase/10634/11 "2024-09-09T08:32:17Z")

</div>

Hi ! Thank you so much !  
That’s working now !!! 🎉  
Have a good one 😉
