-
-
Notifications
You must be signed in to change notification settings - Fork 146
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
Implement unwrap() to throw query errors instead of returning them #188
Conversation
The rationale and motivation for this change is well described in supabase/supabase-js#92, and further supported by the discussion in supabase/supabase#604. This implementation doesn't actually unwrap the result as described in supabase/supabase-js#92, i.e. returning only the actual data. This is done deliberately for two reasons: 1. Making sure the caller can still extract `count`, `status` and `statusText` while still benefitting from errors being thrown instead of returned 2. Ease the transition from the non-throwing pattern where destructuring is norm anyway, so less code has to be rewritten to take advantage of unwrap Basic tests were added to test/basic.ts and the unwrap function is documented to some degree at least, though this can probably be improved.
const { data } = await supabase.from('my_table').select().unwrap() fwiw, an unwrap pattern doesnt need a const data = await supabase.from('my_table').select().unwrap() it also helps be explicit that an error will be thrown when using unwrap |
Thanks for taking a look @andykais and for your comments! You're absolutely right, Certainly it wouldn't be particularly difficult to actually unwrap the data, but in that case it wouldn't be possible to get Would it make more sense to call the API something other than |
@mstade that sounds reasonable to me. I wasn't aware of other fields like |
LGTM, thanks for the PR @mstade!
That's right, we're working on supabase/supabase-js#170 where non-unwrapped calls should catch all errors, including network errors from
Agreed, this is different from e.g. |
Thanks for the review and comments @soedirgo! 🙏 I went with |
The `unwrap` moniker was a bit of a misnomer, since it didn't actually unwrap the result, and this should make things a bit more clear. This was discussed in a bit more detail in #188.
My apologies for the delay @soedirgo – life got in the way! Anyway, I've pushed the name changed in 3a61484 and also updated the tests and snapshots to reflect the change. Happy to make any further changes should there be a need! As I'm sure you can tell I'm no TypeScript expert, so I'm not sure whether there's a need to change the type signatures, so your guidance is much appreciated! |
All good—thanks again for the PR! :) |
My pleasure, sir! 😊 |
🎉 This PR is included in version 0.31.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@mstade thanks for the super nice PR. As a 15+ yrs java developer, it really hurt not having the API rejecting the promise on error by default but your backwards compatible fix made my day! Just cant live w/o my try/catch haha. |
@logemann no worries chief – thanks for the kind words! 🙏🥰 |
What kind of change does this PR introduce?
This adds an
unwrap()
function on the PostgrestBuilder class, which means errors will be thrown instead of returned.Basic tests were added to test/basic.ts and the unwrap function is documented to some degree at least, though this can probably be improved. This should be a non-breaking change as it just adds functionality, it doesn't change any existing features.
What is the current behavior?
Supabase queries return errors as part of a successful response, i.e. the promise is resolved. The exception (no pun intended) to this is if there's something wrong with the fetch call itself, such as a network error – these will lead to the promise rejecting and can be handled in a try/catch block. This leads to somewhat awkward code:
What is the new behavior?
By adding
unwrap()
to the query, we instruct the client to throw errors instead of returning them, meaning we can simplify the above:Additional context
The rationale and motivation for this change is well described in supabase/supabase-js#92 I think, and also there's some rambling from me around this in supabase/supabase#604.
This implementation doesn't actually unwrap the result as described in supabase/supabase-js#92, i.e. returning only the actual data. This is done deliberately for two reasons:
count
,status
andstatusText
while still benefitting from errors being thrown instead of returnedIt may be worth considering normalizing the network errors in a new major version of the client, so that network errors are also by default returned as part of a resolved promise. That way, you'd have consistent error handling no matter what the issue was – either you use the
{ data, error } = ...
pattern regardless of error type, or you tack onunwrap()
and all errors get thrown. I think that's out of scope for this PR though.