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 !
I’d say maybe just throw simple Code 400 errors and it should work. Works for my functions at least
in your edge function do you handle CORS with the 4xx responses or only with the 2xx?
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 …
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’,
}
Are you sending the CORS headers with the response? Check out the headers: corsHeaders
here you can see me sending both 400 and 405
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.
Ok thank you so much for your answers guys ! I will dig into that
1 Like
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
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 :
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] }```
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
-
check that the current error is actually this specific error returned by the sdk by checking context.workflow.error?.['cause']?.['name']=='FunctionsHttpError'
- the data in the response is accessible in the actions like this
await context.workflow.error.cause.context.json();
Hi ! Thank you so much !
That’s working now !!!
Have a good one