Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Conversation

BigOsvaap
Copy link
Contributor

What kind of change does this PR introduce?

Bug fix.

What is the current behavior?

The fetch method tries to parse error response as JSON before returning. Whenever the error response is not a JSON object, it will not return properly instead a JSON parsing error will be thrown.

What is the new behavior?

The fetch method tries to parse error response as JSON before returning, in case of failure returns a raw text error.

Additional context

Fix #241

Copy link
Member

@soedirgo soedirgo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @BigOsvaap! Left a comment below.

Comment on lines 127 to 134
error = await res.json()

if (error && this.shouldThrowOnError) {
throw error
try {
error = await res.json()
} catch (e) {
error = await res.text()
} finally {
if (error && this.shouldThrowOnError) {
throw error
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't reuse the response body since it was consumed with res.json(). Also, we want the error to be an object with a message property. So how about:

        const body = await res.text()

        try {
          error = JSON.parse(body)
        } catch {
          error = {
            message: body
          }
        }

        if (error && this.shouldThrowOnError) {
          throw error
        }

@soedirgo
Copy link
Member

Hey @BigOsvaap, just realized we urgently need the fix so I'll merge this + my suggestion in. Thanks again for the PR!

@soedirgo soedirgo merged commit eab227d into supabase:master Jan 12, 2022
@github-actions
Copy link

🎉 This PR is included in version 0.35.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fetch method can't handle error response that is not a valid JSON object

2 participants