-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix #20: invoke does not throw on HTTP Error status codes #21
Conversation
invoke() call throws if shouldThrowOnError flag is set
I did a small review of this PR because it is partially addressing a current issue I have with invoke's implementation. Prior to this PR the |
Hi @jaitaiwan |
Thanks for the PR! I think making the error a class is preferred, but can we make it only for the |
I think @jaitaiwan's point is that even if |
@soedirgo However the edge server returns In other words we have no guarantee that the edge server will return a JSON object with well-formatted properties such as |
@soedirgo |
OK, I think I got confused, so ignore what I said. From what I gather, @jaitaiwan's comments agree with the PR - if the edge function responds with 4xx/5xx, the response should be in So the PR makes sense, but some improvements we can make:
Does that look OK? |
So I think the questions we are trying to answer here is : Can we assume that, in case of HTTP status code other than 2xx, the FYI Firebase answered “Yes” to this question by doing 2 things:
Now, in order to do this, Firebase enforces As far as Supabase is concerned though, we can send requests of any type and receive responses of any type. So one solution would be the following
What do you guys think ? |
Ok looks good - and sorry I sent my comments simultaneously Now there are 2 small things I want to confirm: is my understanding correct ? |
Agree on 1. On 2, For TypeScript use, you'd use If you need different response types for 2xx vs. 4xx/5xx responses, you'd need to do |
Ok so additional questions here
|
|
Sorry wasn’t clear here on ES2022 |
Hmm not sure if that would work: link. Honestly I'm not too familiar with how tsc "downlevels" the newer JS features. There's also still the issue of |
OK you are right, then I think everything clear on my side |
- rename HttpError to FunctionsError - pull out status & statusText from FunctionsError - status and statusText are optional
@soedirgo Just pushed the refactor. I realized that we are now in a weird situation where
On balance I believe this is also the way it works for the |
Hey @vejja, thanks so much for this PR! We've discussed this a bit more internally and are actually contemplating moving the behaviour of The Would love to get your feedback on this approach, would that work for you? |
My feeling on this is, that there be two functions:
My preference would be that they are the same function but once you’ve used the body in response.json() for example, you can’t use other body functions (in my understanding). Overall I’d like to see supabase be more opinionated about edge function structure and how it’s called etc to reduce the mental workload for folks newer to the platform, and then ofc the power of the platform would be that you’re not forced to use that pattern. My reasoning on this is that there are a few common use cases which edge functions address which are caused by the nature of the supabase JavaScript library. For example, inviteUserByEmail is a server-side only function so it has to be called somewhere on a server. If you’re wanting to make supabase a server less platform so-to-speak then your two options are Postgres functions with http enabled or edge functions. |
Hi @thorwebdev Funny because I've been turning my head around the same question for the last few days ! But in short I would advocate not to go this way and here is why:
I would like to elaborate a bit on my point number 1 above, which is ultimately the most crucial to me. First, I'd like to testify how superior the client-side DX is with Supabase.Think about Firebase's horrendous modular library where you need to import In comparison, the Supabase syntax of In my case, it played a huge part in adopting Supabase quickly, so I would try as hard as I can to stand with this approach in the Now to the heart of DX with
|
Hey @jaitaiwan I just wrote a very very detailed comment which I think goes exactly your way, i.e.
Your comment is more concise but I agree 100% |
@vejja YES! Yours is fantastic. Completely agree with the conclusion and ensuring we have a unified api throughout the codebase. |
Hi all, |
Has been addressed in supabase-js v2. |
What kind of change does this PR introduce?
invoke()
call throws on HTTP errors ifshouldThrowOnError
flag is setFixes #20
What is the current behavior?
invoke()
does not report HTTP error status codes.In that case it returns a clean
{ data, error: null }
object with the 'data' property populated, and the HTTP status code is lost.What is the new behavior?
invoke()
checks if HTTP status code is other than 2xx, then reports this as an error with the HTTP status code populated.In addition, if the
shouldThrowOnError
flag is set,invoke()
will throw rather than returning the{data, error}
object.This is more in line with the behavior of similar libraries (e.g. Firebase).
Additional context
The proposed PR also exports an
HttpError
class that can be usedif (error instanceof HttpError) {}
if (error.statusCode === xxx) {}
error.data
property