Skip to content

Commit

Permalink
send missing context/page errors as json (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsnq authored Sep 11, 2023
1 parent 296b855 commit aa43308
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
21 changes: 11 additions & 10 deletions helpers/middlewares.js
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 4 additions & 5 deletions helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scrapy-puppeteer-service",
"version": "0.2.2",
"version": "0.2.3",
"private": true,
"scripts": {
"start": "node ./bin/www"
Expand Down

0 comments on commit aa43308

Please sign in to comment.