@lokalise/fastify-extras
Version:
Opinionated set of fastify plugins, commonly used in Lokalise
44 lines • 1.39 kB
JavaScript
import { InternalError, isError } from '@lokalise/node-core';
import fp from 'fastify-plugin';
import { stdSerializers } from 'pino';
export const commonErrorObjectResolver = (err, correlationId) => {
return {
...stdSerializers.err(err),
'x-request-id': correlationId,
};
};
function handler(app, opts, err) {
const logObject = opts.errorObjectResolver(err);
app.log.fatal(logObject, 'uncaught exception detected');
opts.errorReporter.report({
error: err,
});
if (opts.shutdownAfterHandling) {
// shutdown the server gracefully
app.close(() => {
process.exit(1); // then exit
});
}
}
function plugin(app, opts, done) {
// Handle unhandled exceptions
process.on('unhandledRejection', (err) => {
const error = isError(err)
? err
: new InternalError({
errorCode: 'UNHANDLED_REJECTION',
message: 'Unhandled rejection',
details: {
errorObject: JSON.stringify(err),
},
});
handler(app, opts, error);
});
process.on('uncaughtException', (err) => handler(app, opts, err));
done();
}
export const unhandledExceptionPlugin = fp(plugin, {
fastify: '5.x',
name: 'unhandled-exception-plugin',
});
//# sourceMappingURL=unhandledExceptionPlugin.js.map