Description
Bug report
Describe the bug
When shouldThrowOnError
is set on supabaseClient, errors raised from functions.invoke() do not throw.
Additional context
@supabase/functions uses the standard fetch API, which does not throw on HTTP status codes other than 2xx; in addition the wrapper does not return the HTTP status code in the response object which is always in the form { data, error }
.
For instance, if the Edge Function was written to reject some unauthorized request by returning a 403 Forbidden
response, it is impossible to catch this condition.
In other words, supabaseClient always returns { data, error: null }
, even when the HTTP status code indicates an error (unless of course if fetch itself threw in the first place).
This forces the user to provide an additional 'httpError' key in the 'data' object, which feels convoluted and obliges to check errors at two different levels (in error and in data.httpError).
Proposed improvement
Modify the code of @supabase/functions:
1- Detect http status codes other than 2xx, and populate the error property in that case together with the status code, instead of populating the data property
2- Add an option to the invoke() function parameters to pass a shouldThrowOnError boolean flag. If set, throw rather than returning the { data, error } object
I can submit a PR if the above makes sense.