decentraland / builder-client

Apache License 2.0
3 stars 5 forks source link

Add more information when a fetch fails #30

Open leanmendoza opened 2 years ago

leanmendoza commented 2 years ago

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 commented 2 years ago

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