Skip to content

Commit aa43308

Browse files
authored
send missing context/page errors as json (#38)
1 parent 296b855 commit aa43308

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

helpers/middlewares.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
exports.exceptionMiddleware = async function exceptionMiddleware(err, req, res, next) {
2-
if (!err.contextId) {
3-
err.contextId = req.query.contextId;
4-
err.pageId = req.query.pageId;
2+
if (res.headersSent) {
3+
return next(err);
54
}
65

7-
if (!err.contextId) {
8-
next(err);
9-
}
6+
const contextId = err.contextId || req.query.contextId;
7+
const pageId = err.pageId || req.query.pageId;
8+
const errorMessage = err.message || 'Unknown error';
109

1110
res.status(500);
12-
res.header('scrapy-puppeteer-service-context-id', err.contextId);
11+
if (contextId) {
12+
res.header('scrapy-puppeteer-service-context-id', contextId);
13+
}
1314
res.send({
14-
'contextId': err.contextId,
15-
'pageId': err.pageId,
16-
'error_msg': err.message,
15+
contextId,
16+
pageId,
17+
error: errorMessage
1718
});
1819
next(err);
1920
}

helpers/utils.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function findContextInBrowser(browser, contextId) {
77
return context;
88
}
99
}
10-
throw "Context not found";
10+
throw new Error("Context not found");
1111
}
1212

1313
async function findPageInContext(context, pageId) {
@@ -16,7 +16,7 @@ async function findPageInContext(context, pageId) {
1616
return page;
1717
}
1818
}
19-
throw "Page not found";
19+
throw new Error("Page not found");
2020
}
2121

2222
exports.closeContexts = async function closeContexts(browser, contextIds) {
@@ -48,7 +48,7 @@ async function wait(page, waitFor) {
4848
}
4949

5050
if ([selector, xpath, timeout].filter(Boolean).length > 1) {
51-
throw "Wait options must contain either a selector, an xpath or a timeout";
51+
throw new Error("Wait options must contain either a selector, an xpath or a timeout");
5252
}
5353

5454
if (selector) {
@@ -173,8 +173,7 @@ exports.performAction = async function performAction(request, action) {
173173

174174
try {
175175
return await action(page, request);
176-
}
177-
catch (err) {
176+
} catch (err) {
178177
err.contextId = page.browserContext().id;
179178
err.pageId = page.target()._targetId;
180179
throw err;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scrapy-puppeteer-service",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"private": true,
55
"scripts": {
66
"start": "node ./bin/www"

0 commit comments

Comments
 (0)