MunifTanjim / node-bitbucket

Bitbucket API client for Browser and Node.js
https://bitbucketjs.netlify.app
MIT License
105 stars 31 forks source link

[HTTPError]: network timeout at: https://api.bitbucket.org/2.0/user #125

Closed TheeMattOliver closed 2 years ago

TheeMattOliver commented 2 years ago

Hi Munif! 👋

Thanks for making this great library!

Had a quick question for you about async...await patterns and using the API.

I need to write a method to recursively create a tree from the contents of a Bitbucket repo, similar to the Github API createTree method or Gitlab/gitbeaker's repositories.tree method. (If you felt like tackling that that would be amazing but I don't think it exists as a raw Bitbucket API endpoint 😅 ).

To quickly start a basic proof of concept I just whipped up a quick NextJS app and am using api routes, but before I got too far I just wanted to solicit some feedback from you about using async...await and this error in particular:

So to start out we have a basic function that just checks the currently authed user using getAuthedUser, and we wrap in try catch:

export default async function handler(req, res) {
    const currentUser = await bitbucket.users.getAuthedUser({});
  try {
    // do some other stuff if we have a currentUser
  } catch (e) {
    return e;
  }
}

That results in the following error:

error - i [HTTPError]: network timeout at: https://api.bitbucket.org/2.0/user
    at /Users/matthewoliver/Desktop/code-certs/bitbucket-api/read-bitbucket-repo/node_modules/bitbucket/lib/index.umd.js:15:6760
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async handler (webpack-internal:///(api)/./pages/api/read-bitbucket-repo.js:26:25)
    at async Object.apiResolver (/Users/matthewoliver/Desktop/code-certs/bitbucket-api/read-bitbucket-repo/node_modules/next/dist/server/api-utils/node.js:179:9)
    at async DevServer.runApi (/Users/matthewoliver/Desktop/code-certs/bitbucket-api/read-bitbucket-repo/node_modules/next/dist/server/next-server.js:381:9)
    at async Object.fn (/Users/matthewoliver/Desktop/code-certs/bitbucket-api/read-bitbucket-repo/node_modules/next/dist/server/base-server.js:497:37)
    at async Router.execute (/Users/matthewoliver/Desktop/code-certs/bitbucket-api/read-bitbucket-repo/node_modules/next/dist/server/router.js:213:36)
    at async DevServer.run (/Users/matthewoliver/Desktop/code-certs/bitbucket-api/read-bitbucket-repo/node_modules/next/dist/server/base-server.js:616:29)
    at async DevServer.run (/Users/matthewoliver/Desktop/code-certs/bitbucket-api/read-bitbucket-repo/node_modules/next/dist/server/dev/next-dev-server.js:532:20)
    at async DevServer.handleRequest (/Users/matthewoliver/Desktop/code-certs/bitbucket-api/read-bitbucket-repo/node_modules/next/dist/server/base-server.js:317:20) {
  error: undefined,
  headers: {},
  request: {
    method: 'GET',
    url: 'https://api.bitbucket.org/2.0/user',
    body: undefined,
    headers: {
      accept: 'application/json',
      'user-agent': 'bitbucket.js/2.7.0',
      authorization: 'Basic <SUCCESSFULLY-GENERATED-AUTH-SECRET-WITH-USERNAME-AND-APP-PASSWORD-HERE>'
    },
    request: { timeout: 20, hook: [Function], validate: undefined }
  },
  status: 500,
  page: '/api/read-bitbucket-repo'
}

If we don't wrap the call to the API in the try/catch like this:

 try {
    const currentUser = await bitbucket.users.getAuthedUser({});
    // do stuff
  } catch (e) {
    return e;
  }
}

We get:

API resolved without sending a response for /api/read-bitbucket-repo, this may result in stalled requests, which indicates that we should be returning a Promise and resolving/rejecting it.

What would you suggest here? I believe there's something I am missing between Promises, try...catch, and async await patterns, and just wanted to get your take on it.

You can try this out locally with this minimal repro.

MunifTanjim commented 2 years ago

Hey @TheeMattOliver, sorry for the late reply.

I tried this code:

async function test() {
  const bitbucket = Bitbucket({
    auth: {
      username: 'MunifTanjim',
      password: '<app-password>',
    },
  })

  const currentUser = await bitbucket.users.getAuthedUser({})
  console.log(JSON.stringify(currentUser, null, 2))
}

test()

And it worked as expected.

Looking at the error [HTTPError]: network timeout at: https://api.bitbucket.org/2.0/user, it seems like your machine's network can't reach api.bitbucket.org for some reason... maybe you're behind a firewall?

I don't think this is an issue with the library.

TheeMattOliver commented 2 years ago

Thanks! Yeah, it's interesting, I'm not behind a firewall. I'm actually wondering if this has to do with something specific to the way I'm formatting the request in NextJS, but either way, yes, not an issue with the library so I closed it. Appreciate your taking a look!

MunifTanjim commented 2 years ago

I see that you're setting 20ms as timeout: https://github.com/TheeMattOliver/read-bitbucket-repo/blob/7f1635065325eb0737f04628a4bac9dff54eab81/pages/api/read-bitbucket-repo.js#L7

That's a very small time for a request. Try removing that timeout maybe?

TheeMattOliver commented 2 years ago

That worked.

With the NextJS API routes, it looks like this:

export default async function handler(req, res) {
  const currentUser = await bitbucket.users.getAuthedUser({});
  res.status(200).json({ currentUser: currentUser });
  try {
    // do stuff
  } catch (e) {
    return e;
  }
}