Extract Errors.redactSensitivePaths

This commit is contained in:
Daniel Gasienica 2018-03-06 12:46:08 -05:00
parent 33bbb12626
commit 0e2f8a8a06

View file

@ -2,7 +2,8 @@
const Path = require('path');
const toError = require('ensure-error');
const ensureError = require('ensure-error');
const isString = require('lodash/isString');
const APP_ROOT_PATH = Path.join(__dirname, '..', '..', '..');
@ -10,8 +11,16 @@ const APP_ROOT_PATH_PATTERN = new RegExp(APP_ROOT_PATH, 'g');
// toLogFormat :: Error -> String
exports.toLogFormat = (error) => {
const normalizedError = toError(error);
const stackWithoutPrivatePaths =
normalizedError.stack.replace(APP_ROOT_PATH_PATTERN, '<REDACTED_PATH>');
return stackWithoutPrivatePaths;
const normalizedError = ensureError(error);
const stackWithRedactedPaths = exports.redactSensitivePaths(normalizedError.stack);
return stackWithRedactedPaths;
};
// redactSensitivePaths :: String -> String
exports.redactSensitivePaths = (logLine) => {
if (!isString(logLine)) {
return logLine;
}
return logLine.replace(APP_ROOT_PATH_PATTERN, '<REDACTED_PATH>');
};