Skip to content

Commit

Permalink
Prevent the engine from exiting on non-critical SMTP errors from the …
Browse files Browse the repository at this point in the history
…logger (#1109)
  • Loading branch information
Ndpnt authored Oct 17, 2024
2 parents 5963f72 + 962512c commit eb93442
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All changes that impact users of this module are documented in this file, in the [Common Changelog](https://common-changelog.org) format with some additional specifications defined in the CONTRIBUTING file. This codebase adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased [patch]

> Development of this release was supported by the [French Ministry for Foreign Affairs](https://www.diplomatie.gouv.fr/fr/politique-etrangere-de-la-france/diplomatie-numerique/) through its ministerial [State Startups incubator](https://beta.gouv.fr/startups/open-terms-archive.html) under the aegis of the Ambassador for Digital Affairs.
### Fixed

- Prevent the engine from exiting on SMTP errors from the logger

## 2.2.1 - 2024-06-07

_Full changeset and discussions: [#1088](https://github.com/OpenTermsArchive/engine/pull/1088)._
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"simple-git": "^3.8.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0",
"winston": "^3.3.3",
"winston": "^3.9.0",
"winston-mail": "^2.0.0"
},
"devDependencies": {
Expand Down
25 changes: 18 additions & 7 deletions src/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ const alignedWithColorsAndTime = combine(
}),
);

const consoleTransport = new winston.transports.Console(({ silent: process.env.NODE_ENV === 'test' }));
const consoleTransport = new winston.transports.Console({ silent: process.env.NODE_ENV === 'test' });

const transports = [consoleTransport];

const logger = winston.createLogger({
format: alignedWithColorsAndTime,
transports,
rejectionHandlers: transports,
exitOnError: true,
});

logger.on('error', err => {
if ('smtp' in err) { // Check if err has an `smtp` property, even if it's undefined
logger.warn({ message: `Uncaught exception from SMTP mailer detected and treated as an operational error; process will continue running:\n${err.stack}` });

return; // Prevent process exit
}

return process.exit(1); // Exit process for other errors
});

if (config.get('@opentermsarchive/engine.logger.sendMailOnError')) {
Expand All @@ -51,7 +62,6 @@ if (config.get('@opentermsarchive/engine.logger.sendMailOnError')) {
ssl: true,
timeout: 30 * 1000,
formatter: args => args[Object.getOwnPropertySymbols(args)[1]], // Returns the full error message, the same visible in the console. It is referenced in the argument object with a Symbol of which we do not have the reference but we know it is the second one.
exitOnError: true,
};

transports.push(new winston.transports.Mail({
Expand All @@ -67,14 +77,15 @@ if (config.get('@opentermsarchive/engine.logger.sendMailOnError')) {
subject: `[OTA] Inaccessible content — ${os.hostname()}`,
}));
}

logger.configure({
transports,
rejectionHandlers: transports,
});
}
}

logger.configure({
transports,
rejectionHandlers: transports,
exitOnError: true,
});

let recordedSnapshotsCount;
let recordedVersionsCount;

Expand Down

0 comments on commit eb93442

Please sign in to comment.