Skip to content

Commit b00dcab

Browse files
fix(logging): preserve error properties in secure logging
Error objects have non-enumerable name and message properties that were being lost during redaction. Added special handling for Error instances to preserve standard error properties (name, message, stack, code) while still redacting other enumerable properties. Fixes failing test: structured-logger.test.js - error object handling
1 parent a5bb882 commit b00dcab

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/utils/secure-logging.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,24 @@ function redactObject(obj, depth = 0, maxDepth = 10) {
173173
// Handle Date objects
174174
if (obj instanceof Date) return obj;
175175

176+
// Handle Error objects specially to preserve standard properties
177+
if (obj instanceof Error) {
178+
const errorObj = {
179+
name: obj.name,
180+
message: obj.message,
181+
stack: obj.stack,
182+
};
183+
184+
// Include any other enumerable properties (like code)
185+
for (const [key, value] of Object.entries(obj)) {
186+
if (!['name', 'message', 'stack'].includes(key)) {
187+
errorObj[key] = redactObject(value, depth + 1, maxDepth);
188+
}
189+
}
190+
191+
return errorObj;
192+
}
193+
176194
// Handle arrays
177195
if (Array.isArray(obj)) {
178196
return obj.map((item) => redactObject(item, depth + 1, maxDepth));

0 commit comments

Comments
 (0)