Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions helpers/exceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.PageNotFoundError = class PageNotFoundError extends Error {
constructor(message="Page not found", ...args) {
super(message, ...args);
this.message = message;
this.name = "PageNotFoundError";
}
}

exports.ContextNotFoundError = class ContextNotFoundError extends Error {
constructor(message="Context not found", ...args) {
super(message, ...args);
this.message = message;
this.name = "ContextNotFoundError";
}
}
15 changes: 14 additions & 1 deletion helpers/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const exceptions = require("./exceptions");

exports.exceptionMiddleware = async function exceptionMiddleware(err, req, res, next) {
if (res.headersSent) {
return next(err);
Expand All @@ -7,14 +9,25 @@ exports.exceptionMiddleware = async function exceptionMiddleware(err, req, res,
const pageId = err.pageId || req.query.pageId;
const errorMessage = err.message || 'Unknown error';

res.status(500);
if (contextId) {
res.header('scrapy-puppeteer-service-context-id', contextId);
}

if (err.contextId) { // there was a context, but something went wrong
res.status(500);
} else { // No context. Possibly, our service was restarted
if (err instanceof exceptions.PageNotFoundError || err instanceof exceptions.ContextNotFoundError) {
res.status(422);
} else {
res.status(500);
}
}

res.send({
contextId,
pageId,
error: errorMessage
});

next(err);
}
6 changes: 4 additions & 2 deletions helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const exceptions = require("./exceptions");
const { proxyRequest } = require('puppeteer-proxy');

const PROXY_URL_KEY = 'puppeteer-service-proxy-url'

async function findContextInBrowser(browser, contextId) {
Expand All @@ -7,7 +9,7 @@ async function findContextInBrowser(browser, contextId) {
return context;
}
}
throw new Error("Context not found");
throw new exceptions.ContextNotFoundError();
}

async function findPageInContext(context, pageId) {
Expand All @@ -16,7 +18,7 @@ async function findPageInContext(context, pageId) {
return page;
}
}
throw new Error("Page not found");
throw new exceptions.PageNotFoundError();
}

exports.closeContexts = async function closeContexts(browser, contextIds) {
Expand Down