diff --git a/helpers/middlewares.js b/helpers/middlewares.js index e3f3fa5..4f77958 100644 --- a/helpers/middlewares.js +++ b/helpers/middlewares.js @@ -1,19 +1,20 @@ exports.exceptionMiddleware = async function exceptionMiddleware(err, req, res, next) { - if (!err.contextId) { - err.contextId = req.query.contextId; - err.pageId = req.query.pageId; + if (res.headersSent) { + return next(err); } - if (!err.contextId) { - next(err); - } + const contextId = err.contextId || req.query.contextId; + const pageId = err.pageId || req.query.pageId; + const errorMessage = err.message || 'Unknown error'; res.status(500); - res.header('scrapy-puppeteer-service-context-id', err.contextId); + if (contextId) { + res.header('scrapy-puppeteer-service-context-id', contextId); + } res.send({ - 'contextId': err.contextId, - 'pageId': err.pageId, - 'error_msg': err.message, + contextId, + pageId, + error: errorMessage }); next(err); } diff --git a/helpers/utils.js b/helpers/utils.js index 784a45e..0825835 100644 --- a/helpers/utils.js +++ b/helpers/utils.js @@ -7,7 +7,7 @@ async function findContextInBrowser(browser, contextId) { return context; } } - throw "Context not found"; + throw new Error("Context not found"); } async function findPageInContext(context, pageId) { @@ -16,7 +16,7 @@ async function findPageInContext(context, pageId) { return page; } } - throw "Page not found"; + throw new Error("Page not found"); } exports.closeContexts = async function closeContexts(browser, contextIds) { @@ -48,7 +48,7 @@ async function wait(page, waitFor) { } if ([selector, xpath, timeout].filter(Boolean).length > 1) { - throw "Wait options must contain either a selector, an xpath or a timeout"; + throw new Error("Wait options must contain either a selector, an xpath or a timeout"); } if (selector) { @@ -173,8 +173,7 @@ exports.performAction = async function performAction(request, action) { try { return await action(page, request); - } - catch (err) { + } catch (err) { err.contextId = page.browserContext().id; err.pageId = page.target()._targetId; throw err; diff --git a/package.json b/package.json index 117d1b9..6d93c9f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scrapy-puppeteer-service", - "version": "0.2.2", + "version": "0.2.3", "private": true, "scripts": { "start": "node ./bin/www"