Skip to content
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

Add more information when a fetch fails #30

Open
leanmendoza opened this issue Mar 2, 2022 · 1 comment
Open

Add more information when a fetch fails #30

leanmendoza opened this issue Mar 2, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@leanmendoza
Copy link

In this block of code https://github.com/decentraland/builder-client/blob/main/src/client/BuilderClient.ts#L102

    try {
      upsertResponse = await this.fetch(`/v1/items/${item.id}`, {
        headers: { 'content-type': 'application/json' },
        body: JSON.stringify({
          item: { ...item, eth_address: this.getAddress() }
        }),
        method: 'put'
      })
      upsertResponseBody =
        (await upsertResponse.json()) as ServerResponse<RemoteItem>
    } catch (error) {
      throw new ClientError(error.message, undefined, null)
    }

If the fetch fails gracefully and there is no body, it's anyway trying to call the .json(). This always force the error to Unexpected token < in JSON at position 0 and throw away the useful data to know what is happen (for example a Forbidden status code)

@LautaroPetaccio
Copy link
Collaborator

In order to fix this issue, the upsertResponse found here must be checked before trying to get its JSON body.

@nachomazzara nachomazzara added the bug Something isn't working label Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants