Skip to content

Commit

Permalink
fix(ilc/server): skip tailorx seobotstream errors logging
Browse files Browse the repository at this point in the history
  • Loading branch information
stas-nc committed Mar 6, 2024
1 parent 128f1d8 commit 2c3431d
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ node_modules
/dbfiles
/mkdocs_dist
registry/lde/dbfiles
.yalc
yalc.lock
109 changes: 105 additions & 4 deletions ilc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions ilc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test:client": "npm run build:systemjs && cross-env NODE_ENV=test karma start",
"test:client:watch": "npm run test:client -- --single-run=false --auto-watch=true",
"start": "node --max-http-header-size 30000 dist/server/index.js",
"dev": "npm run build:polyfills && cross-env NODE_ENV=development nodemon -w server -w common --exec \"npm run build:server && npm run start\" ",
"dev": "npm run build:polyfills && cross-env NODE_ENV=development nodemon -w server -w common --exec \"npm run build:server && npm run start | pino-pretty\" ",
"build": "rimraf public && npm run build:server && npm run build:systemjs && npm run build:client && npm run build:polyfills",
"build:server": "npx tsc --project ./tsconfig.server.json && npx copyfiles -f ./config/* ./dist/config/",
"build:client": "webpack --config build/webpack.js --progress",
Expand All @@ -22,7 +22,7 @@
"license": "Apache-2.0",
"dependencies": {
"@namecheap/error-extender": "^2.0.0",
"@namecheap/tailorx": "^8.0.0",
"@namecheap/tailorx": "^8.1.0",
"@newrelic/native-metrics": "^10.0.1",
"agentkeepalive": "^4.5.0",
"axios": "^0.21.4",
Expand All @@ -46,6 +46,7 @@
"parseurl": "^1.3.3",
"serve-static": "^1.15.0",
"single-spa": "5.6.0",
"source-map-support": "^0.5.21",
"systemjs": "6.10.1",
"systemjs-css-extra": "^1.0.2",
"url-join": "^4.0.1",
Expand Down Expand Up @@ -82,6 +83,7 @@
"nock": "^13.3.4",
"nodemon": "^3.0.1",
"nyc": "^15.1.0",
"pino-pretty": "^10.3.1",
"rimraf": "^5.0.5",
"sinon": "^16.1.0",
"supertest": "^6.3.3",
Expand Down
16 changes: 8 additions & 8 deletions ilc/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ module.exports = (registryService, pluginManager, context) => {
throw new Error('500 page test error');
});

app.all('*', async (req, res) => {
app.all('*', async (req, reply) => {
const currentDomain = req.hostname;
let registryConfig = await registryService.getConfig({ filter: { domain: currentDomain } });
const url = req.raw.url;
const urlProcessor = new UrlProcessor(registryConfig.settings.trailingSlash);
const processedUrl = urlProcessor.process(url);
if (processedUrl !== url) {
res.redirect(processedUrl);
reply.redirect(processedUrl);
return;
}

Expand All @@ -144,7 +144,7 @@ module.exports = (registryService, pluginManager, context) => {
const redirectTo = await guardManager.redirectTo(req);

if (redirectTo) {
res.redirect(
reply.redirect(
urlProcessor.process(
i18n.localizeUrl(registryConfig.settings.i18n, redirectTo, {
locale: req.raw.ilcState.locale,
Expand All @@ -164,7 +164,7 @@ module.exports = (registryService, pluginManager, context) => {
);

try {
res.res = csp.setHeader(res.res);
reply.res = csp.setHeader(reply.res);
} catch (error) {
errorHandlingService.noticeError(error, {
message: 'CSP object processing error',
Expand All @@ -176,16 +176,16 @@ module.exports = (registryService, pluginManager, context) => {
const locale = req.raw.ilcState.locale;
let { data } = await registryService.getTemplate(route.template, { locale });

res.header('Content-Type', 'text/html');
res.status(200).send(data.content);
reply.header('Content-Type', 'text/html');
reply.status(200).send(data.content);
return;
}

const slotCollection = new SlotCollection(route.slots, registryConfig);
slotCollection.isValid();

res.sent = true; // claim full responsibility of the low-level request and response, see https://www.fastify.io/docs/v2.12.x/Reply/#sent
tailor.requestHandler(req.raw, res.res);
reply.sent = true; // claim full responsibility of the low-level request and response, see https://www.fastify.io/docs/v2.12.x/Reply/#sent
tailor.requestHandler(req.raw, reply.res);
});

app.setErrorHandler(errorHandlingService.handleError);
Expand Down
1 change: 0 additions & 1 deletion ilc/server/errorHandler/ErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ module.exports = class ErrorHandler {

handleError = async (err, req, res) => {
const errorId = uuidv4();

// This handler serves as Fastify & Tailor handler.
// While Fastify will pass it's own Reply object
// Tailor passes http.ServerResponse from Node core
Expand Down
2 changes: 2 additions & 0 deletions ilc/server/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'source-map-support/register';

const path = require('path');
const { context } = require('./context/context');

Expand Down
39 changes: 18 additions & 21 deletions ilc/server/tailor/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,28 @@ const errors = require('./errors');
* @param {Tailor} tailor
* @param {ErrorHandler} errorHandlingService
*/

const IGNORED_ERRORS = ['SeobotsGuardStreamError'];

module.exports = function setup(tailor, errorHandlingService) {
//TODO: Handle Bot specific behaviour
function handleError(req, err, res) {
function handleError(req, error, res) {
const urlPart = `while processing request "${req.originalUrl}"`;
if (res !== undefined) {
// If fragment respond with 404 - force special 404 route
// See "process-fragment-response.js", there we throw this error
if (err.cause instanceof errors.Fragment404Response) {
req.ilcState.forceSpecialRoute = '404';
tailor.requestHandler(req, res);
return;
}
// If fragment respond with 404 - force special 404 route
// See "process-fragment-response.js", there we throw this error
if (error.cause instanceof errors.Fragment404Response) {
req.ilcState.forceSpecialRoute = '404';
tailor.requestHandler(req, res);
return;
}

const wrappedError = new errors.TailorError({ message: `Tailor error ${urlPart}`, cause: error });
const shouldWriteResponse = Boolean(res);

const e = new errors.TailorError({ message: `Tailor error ${urlPart}`, cause: err });
errorHandlingService.handleError(e, req, res).catch((err) => {
errorHandlingService.noticeError(
new errors.TailorError({
message: 'Something went terribly wrong during error handling',
cause: err,
}),
);
});
} else {
if (shouldWriteResponse) {
errorHandlingService.handleError(wrappedError, req, res);
} else if (!IGNORED_ERRORS.includes(error.name)) {
errorHandlingService.noticeError(
new errors.TailorError({ message: `Tailor error while headers already sent ${urlPart}`, cause: err }),
wrappedError,
{ userAgent: req.headers['user-agent'] },
{ reportError: !req.ldeRelated },
);
Expand Down
18 changes: 0 additions & 18 deletions ilc/server/tailor/error-handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,6 @@ describe('error handler', () => {
]);
});

it('should notice Tailor Error when something went wrong during error handling', async () => {
const response = {};
const somethingWentWrongDuringErrorHandling = new Error('something went wrong during error handling');

errorHandlingService.handleError.onFirstCall().rejects(somethingWentWrongDuringErrorHandling);
eventHandlers.get('error')(request, error, response);

await clock.runAllAsync();

chai.expect(errorHandlingService.noticeError.calledOnce).to.be.true;
chai.expect(errorHandlingService.noticeError.getCall(0).args).to.be.eql([
new errors.TailorError({
message: 'Something went terribly wrong during error handling',
cause: somethingWentWrongDuringErrorHandling,
}),
]);
});

it('should handle Tailor Error when headers did not send yet and error handling is going to be successful', async () => {
const response = {};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"concurrently": "^8.2.1",
"husky": "^8.0.3",
"prettier": "^3.0.3"
}
},
"dependencies": {}
}

0 comments on commit 2c3431d

Please sign in to comment.