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

Node Crashes due to Dangling Promise? #20

Open
chrisrosner opened this issue Aug 23, 2023 · 1 comment
Open

Node Crashes due to Dangling Promise? #20

chrisrosner opened this issue Aug 23, 2023 · 1 comment

Comments

@chrisrosner
Copy link

Hey folks, maybe I'm hitting my limits of js/node.js knowledge, but I am having trouble understanding how to catch certain classes of error with this library in an async/promise context.

Specifically, node.js crashes if you pass an empty string query to the json method, eg:

let client = new serpapi.GoogleSearchResults(api_key)
client.json({}, (data) => {done();})

It looks like there used to be a unit test for this, but it has been removed.

10aadf8
"xit("fail:json", (done) => {"

Looking at the implementation, https://github.com/serpapi/google-search-results-nodejs/blob/master/lib/SerpApiSearch.js#L89C11-L89C16, it seems that any response from google that is not a 200 response will throw an exception that is impossible to catch, leading to dangling promises and node.js crashing.

Is the library broken? Is there a different pattern I should use to catch these exceptions?

I'm using the suggested pattern from this page: https://serpapi.com/integrations/node#promise-and-callback

const getJson = () => {
return new Promise((resolve) => {
search.json(params, resolve);
});
};

@ilyazub
Copy link

ilyazub commented Jun 28, 2024

Hi @chrisrosner. I'm sorry for the late reply.

You can handle errors in the getJson function:

function getJson(params) {
  return new Promise((resolve, reject) => {
    try {
      search.json(params, resolve)
    } catch (e) {
      reject(e)
    }
  })
}

Related to #4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants