supabase / supabase-swift

A Swift client for Supabase

Home Page:https://supabase.com/docs/reference/swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DecodingError when receiving 401 from API

leoMehlig opened this issue · comments

Bug report

Describe the bug

I've noticed some users sometimes getting NSCocoaErrorDomain - The given data was not valid JSON. (Code: 4864) errors. After some investigation, this seems to happen if requests are made with an expired auth token and the API returns a 401 response with no body (which goes a decoding error in PostgrestBuilder.swift:114).

I think for normal users this can happen, if their system clock is not 100% correct and is running behind a bit.

To Reproduce

  • Set a very short refresh interval on the server (like 30 seconds)
  • Setback your test device's time by 1 min
  • Log in, wait 30 seconds & make any database API request
    → the request will fail with the error mentioned above.

Expected behavior

I believe the SDK should either throw a correct error (401 API error in this case) or even attempt to refresh the token and redo the request.

Thanks @leoMehlig I'll investigate

@leoMehlig I'd love to talk about how you're using the library, and the main pain points you're having with it.

Thanks @grdsdev! Sure, we are happy to do that. What channel works best for you here?

On Supabase's Discord server https://discord.gg/zwaJcTSF, I'm @grdsdev there.

@grdsdev I've noticed that we are running into the same problem when the project is not running (upgrading or paused). In that case, the API returns status code 540, but the SDK tries to parse a non-existent JSON. For that reason, we can't handle that error correctly on the client.

I think the easiest solution here would be to throw an APIError with the status code if the JSON can't be parsed. I'm happy to submit an PR for that if you agree with that approach.