-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Base * separated middlewares * readme * Too many requests error * Now limit_context is a small service. * Typos * Typos * Delete useless variable * Version update
- Loading branch information
1 parent
ca1e25c
commit 615340c
Showing
10 changed files
with
122 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
let contextCounter = 0; | ||
|
||
function incContextCounter() {} | ||
exports.incContextCounter = incContextCounter; // Empty function or incrementer | ||
|
||
function decContextCounter() {} | ||
exports.decContextCounter = decContextCounter; // Empty function or decrementer | ||
|
||
function canCreateContext() { return true; } | ||
exports.canCreateContext = canCreateContext; // Truish function or checker if the context can be created | ||
|
||
exports.initContextCounter = function (maxContextCounter) { | ||
if (!isNaN(maxContextCounter)) { | ||
exports.incContextCounter = () => { contextCounter++ }; | ||
exports.decContextCounter = () => { contextCounter-- }; | ||
exports.canCreateContext = () => { return contextCounter < maxContextCounter } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports.logHTTPMiddleware = require('./logging').logHTTPMiddleware; | ||
exports.logExceptionMiddleware = require('./logging').logExceptionMiddleware; | ||
exports.processExceptionMiddleware = require('./process_exception').processExceptionMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const loggers = require("../loggers"); | ||
const morgan = require("morgan"); | ||
|
||
/*** | ||
* Returns the middleware for logging HTTP request-response. | ||
***/ | ||
exports.logHTTPMiddleware = function logHTTPMiddleware() { | ||
const logger = loggers.getLogger(); | ||
|
||
return morgan( | ||
loggers.HTTPFormat, | ||
{ | ||
stream: { | ||
write: (message) => logger.http(message), | ||
}, | ||
} | ||
); | ||
} | ||
|
||
/*** | ||
* Middleware for logging exceptions. | ||
***/ | ||
exports.logExceptionMiddleware = async function logExceptionMiddleware(err, req, res, next) { | ||
loggers.getLogger().error({ | ||
message: err, | ||
contextId: req.query["contextId"], | ||
pageId: req.query["pageId"], | ||
}); | ||
next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const exceptions = require("../exceptions"); | ||
|
||
/*** | ||
* Middleware for processing exceptions. | ||
***/ | ||
exports.processExceptionMiddleware = async function processExceptionMiddleware(err, req, res, next) { | ||
if (res.headersSent) { | ||
return next(err); | ||
} | ||
|
||
const contextId = err.contextId || req.query.contextId; | ||
const pageId = err.pageId || req.query.pageId; | ||
const errorMessage = err.message || 'Unknown error'; | ||
|
||
if (contextId) { | ||
res.header('scrapy-puppeteer-service-context-id', contextId); | ||
} | ||
|
||
if (err instanceof exceptions.TooManyContextsError) { | ||
res.status(429); // Too Many Requests | ||
} else 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); // Unprocessable Entity | ||
} else { | ||
res.status(500); | ||
} | ||
} | ||
|
||
res.send({ | ||
contextId, | ||
pageId, | ||
error: errorMessage | ||
}); | ||
|
||
next(err); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters