Skip to content

Commit

Permalink
DeadContext handling (#39)
Browse files Browse the repository at this point in the history
* Added processing of accidental context losses

* Deleted extra middleware

* Added custom errors for more flexible development.
Now we response with not only 422 but also with 500

* Major logic of middleware:
set status and send response
  • Loading branch information
MatthewZMSU authored Dec 8, 2023
1 parent 07d49b9 commit cc6ff31
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
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

0 comments on commit cc6ff31

Please sign in to comment.