xataio / client-ts

Xata.io SDK for TypeScript and JavaScript
https://xata.io/docs/sdk/typescript/overview
Apache License 2.0
123 stars 8 forks source link

Unexpected 403 in client #407

Closed TejasQ closed 2 years ago

TejasQ commented 2 years ago

I'm experiencing an error from the client and I'm not sure why. 🧐 It worked the first time when bulk importing, but subsequent requests error.

Error

image
error - FetchError: invalid json response body at https://xata-examples-49fn2g.xata.sh/db/aita:main/tables/posts/data/t3_vvnjbw reason: Unexpected end of JSON input
    at /Users/tejas/Sites/LAB/aita/node_modules/next/dist/compiled/node-fetch/index.js:1:51227
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async fetch$1 (file:///Users/tejas/Sites/LAB/aita/node_modules/@xata.io/client/dist/index.mjs:162:26)
    at async RestRepository.upsertRecordWithID_fn (file:///Users/tejas/Sites/LAB/aita/node_modules/@xata.io/client/dist/index.mjs:1486:20)
    at async RestRepository.createOrUpdate (file:///Users/tejas/Sites/LAB/aita/node_modules/@xata.io/client/dist/index.mjs:1344:22)
    at async Promise.all (index 21)
    at async handler (webpack-internal:///(api)/./pages/api/getData.ts:18:5)
    at async Object.apiResolver (/Users/tejas/Sites/LAB/aita/node_modules/next/dist/server/api-utils/node.js:179:9)
    at async DevServer.runApi (/Users/tejas/Sites/LAB/aita/node_modules/next/dist/server/next-server.js:381:9)
    at async Object.fn (/Users/tejas/Sites/LAB/aita/node_modules/next/dist/server/base-server.js:497:37) {
  status: 403,
  errors: undefined,
  cause: undefined,
  page: '/api/getData'
}

Code

const handler: NextApiHandler = async (_, res) => {

    // Get and store posts
    const posts = await fetch('https://www.reddit.com/r/amitheasshole.json')
        .then(r => r.json())
        .then(({ data: rootData }) => rootData.children
            .map(({ data }) => ({ id: data.name, title: data.title, content: data.selftext }))
        );

    await client.db.posts.createOrUpdate(posts)
    res.end(posts);
}

Screenshot

image

Usages

https://github.com/xataio/aita-to-xata/blob/bb5d16ad0c697065ad7f9151439439dceadb4bde/pages/api/getPosts.ts#L16

https://github.com/xataio/aita-to-xata/blob/bb5d16ad0c697065ad7f9151439439dceadb4bde/pages/api/getComments.ts#L27

TejasQ commented 2 years ago

Nevermind, I'll investigate further and reopen this. Error may be in my code.

TejasQ commented 2 years ago

So it actually inserts data, but it fails somewhere later with this JSON error "Unexpected end of JSON input".

TejasQ commented 2 years ago

This also happens with other internal requests in the client randomly:

image
gimenete commented 2 years ago

@TejasQ Can you coordinate with @xataio/backenders to try to find traces of those requests? Seems like a backend bug to me.

SferaDev commented 2 years ago

I didn't like the gitBranch=undefined from his screenshot. Opening a PR to fix that.

image

SferaDev commented 2 years ago

Closing this issue as it's from the backend.

We have added the requestId to the error message in the SDK so that we can trace them back to datadog and get more information.