dijs / wiki

Wikipedia Interface for Node.js
MIT License
315 stars 61 forks source link

Unhandled promise rejection #50

Closed nnnikolay closed 7 years ago

nnnikolay commented 7 years ago

Hi there,

What is the best practice to handle the case when requested page does not exist?

In this case, I see following message: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'revisions' of undefined

for example:

          wiki()
            .page('asdasdlkalskdjalsdjalskdalsdjasdasd')
            .then(page => {
                page.html().then(d => console.log(d));
           })
           .catch(e => console.error(e));
dijs commented 7 years ago

Good find, we should handle that better!

dijs commented 7 years ago

Also, try returning that second promise created with the page.html()

nnnikolay commented 7 years ago

... and one more note in wiki.js in the page function you do this

const id = Object.keys(res.query.pages)[0];
if (!id) {

The problem is, sometimes (especially when there is no such page) there will be "-1" in id, therefore throw will not happen