auth-js icon indicating copy to clipboard operation
auth-js copied to clipboard

FetchError: Unexpected token < in JSON at position 0

Open noskovvkirill opened this issue 2 years ago • 19 comments

Bug report

Describe the bug

Hi guys! I'm using Supabase in one of my projects and I face a really weird issue. First I thought it's related to the JS version of the superbase-js library (some people reported the same error). Updating the client fixed that, but then the issue reappeared again. I see error { message: 'FetchError: invalid json response body at https://tcmqmkigakxeiuratohw.supabase.co/rest/v1/mirroritems_test?select=*&order=timestamp.desc.nullslast&limit=19 reason: Unexpected token < in JSON at position 0', details: '', hint: '', code: '' }

To Reproduce

I use the alpha preview of functions. The last time Supabase was down it coincide with the function that was triggered. Not sure if it caused an issue or not. If I mock the data and trigger the function myself, it works correctly. I don't have a clue how to reproduce it exactly, it seems to me that it's not the issue with the client or framework I use. But just in case, I use next.js "11.1.2" with "@supabase/supabase-js": "^1.28.5",

Restarting the server seems to fix the issue.

Expected behavior

Stable runtime.

Screenshots

issue

System information

  • OS: macOS
  • Version of supabase-js 1.28.5
  • Version of Node.js: v14.15.1

noskovvkirill avatar Dec 13 '21 14:12 noskovvkirill

very odd - it seems that it's trying to decode the response but the response is not being returned as JSON. Are you able to give us some steps to reproduce this @noskovvkirill ?

kiwicopple avatar Dec 14 '21 08:12 kiwicopple

@kiwicopple Not really, not sure what is happening exactly. I will try to monitor it and see if I can reproduce it. It happened two times within the last couple of days. I thought it's related to https://github.com/supabase/supabase-js/pull/305, but the client update didn't fix that.

noskovvkirill avatar Dec 14 '21 13:12 noskovvkirill

@kiwicopple It happens again 😅 This time database reload doesn't help. I've tried to fetch different tables, but the issue seems to appear independent of the content! API page is not displayed on Supabase own front-end. Снимок экрана 2021-12-15 в 13 46 59

Снимок экрана 2021-12-15 в 14 19 58

The website is currently down. Only cached pages are working. Help 🙏

noskovvkirill avatar Dec 15 '21 12:12 noskovvkirill

@noskovvkirill - can you please submit a support ticket here: https://app.supabase.io/support/new

Thanks!

kiwicopple avatar Dec 16 '21 06:12 kiwicopple

Having a similar issue.

0hq avatar Dec 29 '21 22:12 0hq

had the same issue until i turned off RLS

edwardz8 avatar Jan 08 '22 19:01 edwardz8

Turning off RLS had no effect - still running into the issue randomly.

timuster avatar Jan 11 '22 02:01 timuster

Hi all 👋

In order to get a more helpful error response, we have recently updated our JS libraries (to get in the fixes for this issue in the supabase/postgrest-js library via this PR). Would you mind taking the following steps to upgrade it:

  1. Run npm run update within your directory.
  2. @supabase/postgrest-js should then now be in version 0.35.1 or above. To verify this, you can run npm list @supabase/postgrest-js.

This won't solve the issue at hand but at the very least we should now get a less vague, and hopefully more insightful response than Unexpected token < in JSON at position 0, and should eventually direct all of us to the root cause/s.

dragarcia avatar Jan 12 '22 08:01 dragarcia

I'll close this for now as this issue should be solved on the latest library version

kiwicopple avatar Jan 12 '22 14:01 kiwicopple

Got the same issue yesterday around 11:20 IST

satyapalsharma avatar May 09 '22 01:05 satyapalsharma

Got the same issue today

redareda9 avatar May 27 '22 03:05 redareda9

I got the same issue today

thuongdinh avatar May 27 '22 03:05 thuongdinh

FYI, we're tracking this one in https://github.com/supabase/postgrest-js/issues/282

steve-chavez avatar May 27 '22 21:05 steve-chavez

Just experienced the same issue and it caused my server to crash. I believe it happened as a result of calling:

const { user, error } = await supabase.auth.api.getUser(token)
// Error message:
// FetchError: invalid json response body at https://fmapsufvoocumtvhyody.supabase.co/auth/v1/user 
// reason: Unexpected token < in JSON at position 0

// This line within node-fetch threw the error:
// return Body.Promise.reject(new FetchError(`invalid json response body at ${_this2.url} reason: ${err.message}`, 'invalid-json'));

While the root of this bug is being fixed, in the meantime is there some way to capture the error and reveal it in the returned .error prop of the supabase response object instead of allowing the supabase SDK method to throw?

I assume most codebases will not expect supabase methods to throw... this is what caused my server to crash. If the error was passed along via the error prop, my code would have handled it properly.

Right now, I'm forced to wrap all of my supabase SDK API methods in a try-catch which is redundant & messy.

image

miketromba avatar Jul 07 '22 16:07 miketromba

I've repeatedly run into this problem while trying to do bulk operations on auth.api.getUser / auth.api.createUser to synchronize a new Supabase project with a (for now) external source of truth on users. Even when rate-limiting my operations to a max of 20 in parallel, I have to put extensive retry logic on anything that touches auth.api.

I seriously don't understand what it's doing, because at this point I could probably just start direct inserting into the users table instead and have more success, and I've already started using a view on users as a workaround instead of using getUser.

icopp avatar Jul 14 '22 22:07 icopp

@miketromba @icopp Though the error message is the same, the issue you mention comes from the Auth API(GoTrue) and that's not used by postgrest-js.

Perhaps you could open an issue on https://github.com/supabase/gotrue-js/ for better visibility?

steve-chavez avatar Jul 14 '22 22:07 steve-chavez

This seems to be a class of errors rather than a single isolated error some of which arise from the createUser/getUser endpoints. Moving to Auth repo to triage

J0 avatar Jul 18 '22 08:07 J0

@J0 In my understanding deep down it is all related to a single problem. Where database is not responding in timely manner, Thus causing the related API to timeout. As Supabase client library always expect API response to be a valid JSON it is thrwoing errors on not getting one. But these are all related to the API timeout.

There is something seriously wrong with the memory management/Reporting at supabase end. Anyone who is facing these issues just Increase the RAM and 90% of these issues will be gone, No matter how much RAM is free on your server.

satyapalsharma avatar Jul 18 '22 19:07 satyapalsharma

ran into same issue today! :|

mohitxskull avatar Sep 17 '22 19:09 mohitxskull

Hey everyone, just wanna check if this issue has been fixed? We noticed an issue with our internal api-gateway where there was an error parsing certain JWTs sent. We've since rectified the issue awhile back.

I don't think that this is an issue with the gotrue-js client library as I can't seem to reproduce it while running gotrue locally and directly connecting to it via my client app.

If anyone is still facing this issue, can you please reach out to us at support and we'll get to the bottom of it!

kangmingtay avatar Sep 30 '22 05:09 kangmingtay

Hey everyone, just want to give some context on errors that say Unexpected token < in JSON at position 0. This is a class of errors that occurs usually when the underlying server is down and the CDN is rendering a 5XX response page with HTML. The error comes because all HTML starts with <!DOCTYPE....

If you see this, it's probably not connected to the way you're using the API and means that something crashed, is not reachable or timed out.

hf avatar Oct 13 '22 09:10 hf

What fixed it for me was using encodeURIComponent for my storage bucket path that had spaces in it. I assume that if the path isn't encoded properly it hits a bad url and the server responds with a non-json 404 response, hence the < in jSON at position 0

STRd6 avatar Aug 11 '23 03:08 STRd6