diff --git a/index.js b/index.js index 12c173a..e0c9d93 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const express = require("express") , browser = require('./src/service/HeadlessService') , port = 32101 , { NoSuchSessionError } = require("selenium-webdriver/lib/error") - // , { puppeteerErrors } = require("puppeteer-core/lib/cjs/puppeteer/common/Errors"); +// , { puppeteerErrors } = require("puppeteer-core/lib/cjs/puppeteer/common/Errors"); logger.getContext = function getContext() { @@ -63,12 +63,15 @@ process.on('SIGTERM', exitHandler.bind(null, { cleanup: true })); * request handler */ app.post('/', async (req, res, next) => { - browser.get(req.body).then(html => res.send(html)).catch(e => { - if(e instanceof NoSuchSessionError) { + browser.get(req.body).then(html => { + if (html) res.send(html) + else throw new Error('Emtpy html') + }).catch(e => { + if (e instanceof NoSuchSessionError) { logger.error({ reason: e.message, url: req.url }, 'Browser session expired') - }else { + } else { logger.error({ reason: e.message, url: req.url }, 'Browser error occured') - } + } res.status(500).send({ code: 500, message: e.message }) }) }) diff --git a/src/component/SeleniumBrowser.js b/src/component/SeleniumBrowser.js index e8108e7..44d4af4 100644 --- a/src/component/SeleniumBrowser.js +++ b/src/component/SeleniumBrowser.js @@ -112,6 +112,10 @@ class SeleniumBrowser { .then(html => { this.pool.release(driver) return html + }).catch(err => { + logger.error({message: err.message}, 'Wait element failed') + this.pool.release(driver) + return '' }) } else { this.pool.release(driver)