puppeteer throw error "UnhandledPromiseRejectionWarning" and crash - javascript

I want to take a screenshot of a webpage, but no matter what I do puppeteer always crashes。
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
await page.goto('https://www.google.com/');
await page.screenshot({path: 'screenshot/example.png'});
await page.waitFor(5 * 1000);
await browser.close();
})();
I saved the above code as web.js, and executed node web.js in cmd. It crashed 2 seconds after the Chrome window appeared, and the page did not load。
(node:27064) UnhandledPromiseRejectionWarning: #
(Use node --trace-warnings ... to show where the warning was created)
(node:27064) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:27064) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

you've to add an error handler to your promise.
As soon your promised (in this case your async function) throws any error, it reaches the execution root, which in nodejs will cause the app to "crash".
Just call your function like this:
(async () => {
/* your code here */
})().catch(error => { console.error("Something bad happend...", error); });
``´

Related

tesseract,js error: UnhandledPromiseRejectionWarning / tesseract

I'm using tesseract within my node app. The first time the program runs fine.
In this example, foo() repeats it self. From the first repition on, an error is returned (shown below the code)
Could you give me a hint what I have done wrong which doesn't allow this to run a second time? I have tried to add try & catch, but it only led to more errors
const { createWorker } = require('tesseract.js');
const worker = createWorker();
function foo() {
(async () => {
await worker.load();
await worker.loadLanguage('eng');
await worker.initialize('eng');
await worker.setParameters({
tessedit_char_whitelist: '123456789'
// preserve_interword_spaces: '0',
});
const { data: { text } } = await worker.recognize('Download.png');
console.log(text);
await worker.terminate();
})();
setTimeout(foo, 10000);
}
foo();
Error message:
(node:13988) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of null
at module.exports (\\node_modules\tesseract.js\src\worker\node\send.js:9:10)
at \\node_modules\tesseract.js\src\createWorker.js:47:7
at new Promise (<anonymous>)
at startJob (\\node_modules\tesseract.js\src\createWorker.js:43:5)
at Object.load (\\node_modules\tesseract.js\src\createWorker.js:57:5)
at \\tesseract3.js:28:18
at Timeout.myFetch [as _onTimeout] (\\tesseract3.js:38:5)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13988) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:13988) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:13988) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of null

Getting an unhandled promise rejection when seeding a SQL database

Learning MVC, I'm trying to seed my database with dummy entries and I keep getting this error:
(node:19287) UnhandledPromiseRejectionWarning: Error
at Query.run (/Users/joshuaramat/Documents/projects/tech-blog/node_modules/sequelize/lib/dialects/mysql/query.js:52:25)
at /Users/joshuaramat/Documents/projects/tech-blog/node_modules/sequelize/lib/sequelize.js:313:28
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async MySQLQueryInterface.bulkInsert (/Users/joshuaramat/Documents/projects/tech-blog/node_modules/sequelize/lib/dialects/abstract/query-interface.js:335:21)
at async recursiveBulkCreate (/Users/joshuaramat/Documents/projects/tech-blog/node_modules/sequelize/lib/model.js:1655:25)
at async Function.bulkCreate (/Users/joshuaramat/Documents/projects/tech-blog/node_modules/sequelize/lib/model.js:1744:12)
at async seedAll (/Users/joshuaramat/Documents/projects/tech-blog/seeds/index.js:14:3)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:19287) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:19287) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
the following is the code used to seed my data:
const seedUsers = require('./user-seeds');
const seedPosts = require('./post-seeds');
const seedComments = require('./comment-seeds');
const seedVotes = require('./vote-seeds');
const sequelize = require('../config/connection');
const seedAll = async () => {
await sequelize.sync({ force: true });
console.log('SEQUELIZE--------------');
await seedUsers();
console.log('USERS--------------');
await seedPosts();
console.log('POSTS--------------');
await seedComments();
console.log('COMMENTS--------------');
await seedVotes();
console.log('VOTES--------------');
process.exit(0);
};
seedAll();
The error seems to occur after the seedPosts() function is called.
Would anyone be able to help?

Puppeteer -Getting UnhandledPromiseRejectionWarning: Unhandled promise rejection in Pipelines

I am trying to execute below code,but it throws Unhandled promise rejection warning in the pipelines. Locally it works fine without any issue.
Logs
ERROR: The process "3224" not found.
(node:836) UnhandledPromiseRejectionWarning: #
(node:836) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:836) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Following is the JS code:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
//defaultViewport: null,
headless:false,
slowMo: 500,
timeout: 10000
//slowMo: 500,
//devtools: true,
});
const page = await browser.newPage();
await page.goto('https://www.google.com', {waitUntil: 'networkidle2'});
await page.waitFor(10000);
await browser.close();
})();
The pitfall of using async/await is the need to try/catch your asynchronous call in case of an error, instead of using regular .then().catch()
It happens that developers forget to catch the errors, so node.js outputs UnhandledPromiseRejectionWarning: # (node:836) UnhandledPromiseRejectionWarning: Unhandled promise rejection
I am not sure what the error is though, use below instead to debug:
(async () => {
try {
const browser = await puppeteer.launch({
//defaultViewport: null,
headless: false,
slowMo: 500,
timeout: 10000
//slowMo: 500,
//devtools: true,
});
const page = await browser.newPage();
await page.goto('https://www.google.com', {
waitUntil: 'networkidle2'
});
await page.waitFor(10000);
await browser.close();
} catch (err) {
console.log(err);
}
})();
I saw somewhere that node.js might terminate the process in the future if such unhandled promises occur, I am scared to see this to happen ahah

UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation

So I was watching youtube regarding webscraping and would like to give it a go. I tried mimicking exactly how the tutor teaches but I land into a problem right away.
(node:20976) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
at rewriteError (D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:261:23)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ExecutionContext._evaluateInternal (D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:161:64)
at async D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:90:30
at async DOMWorld.$x (D:\Users\Win81\Desktop\Web-scraper-snkrssg\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:96:26)
at async scrapeProduct (D:\Users\Win81\Desktop\Web-scraper-snkrssg\scraper.js:10:18)
(node:20976) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:20976) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Here is the sample of my codes:
const puppeteer = require('puppeteer');
async function scrapeProduct(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.goto(url)
const [el] = await page.$x('//*[#id="block-1565393554829"]/div/div/div[2]/div/div[1]/a/div/img[2]');
const src = await el.getProperty('src');
const srcTxt = await src.jsonValue();
console.log({srcTxt});
}
scrapeProduct('https://shopnicekicks.com/pages/calendar');
I hope someone could teach me where I'm going wrong.

Nodejs. Unhandled Promise Rejection

I am trying to test some functionality. Inside the routes.js file, i placed this code:
async function getPic(arg) {
const browser = await puppeteer.launch(/*{headless: false}*/);
const page = await browser.newPage();
await page.goto(arg);
await page.setViewport({width: 1000, height: 500})
await page.screenshot({path: 'pic.png'});
await broswer.close();
}
I read about async/await (it is the first time i use it). Though i get an error message and the code does not work:
(node:5896) UnhandledPromiseRejectionWarning: Error: Navigation Timeout Exceeded
: 30000ms exceeded
at Promise.then (C:\...\node_modules\puppeteer\lib\NavigatorWatcher.js:73:21
)
at <anonymous>
(node:5896) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejection
id: 1)
(node:5896) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
Unfortunately i do not know what this means. I found out a similar question on stackoverflow here:
NodeJS Unhandled Promise Rejection
But unfortunately does not make things clear for me on how to solve this error.
When i tested this snippet of code in a standalone node environment - meaning just this code without anything else, as i do in my project, then somehow it works.
When i place this function inside my routes.js file, and then invoke the function when a post event happens then i get the error.
Here is the code that invokes this function:
app.post('/sc', function(req, res){
var url = req.body.convo
console.log(url)
getPic(url);
})
You should use await before call async function and wrapp await call into try catch construction.
app.post('/sc', async (req, res) => {
const url = req.body.convo
try {
var picture = await getPic(url);
//some logic or render response
} catch (error){
//here you should handle error
}
})
You need to await getPic(url);.
Also you have browser misspelled in await broswer.close();

Categories

Resources