Skip to content

Commit

Permalink
fix: preserve err.code
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Jan 13, 2023
1 parent 9af004d commit bde7c3f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,16 @@ function maskProps(obj, props, options) {

// for...in is much faster than Object.entries or any alternative
for (const key in obj) {
if (typeof obj[key] === 'object')
if (typeof obj[key] === 'object') {
// preserve "err.code" property
let code;
if (obj[key] instanceof Error && obj[key].code) code = obj[key].code;
obj[key] = maskProps(obj[key], props, options);
else if (isString(obj[key]))
// reset the masked code value
if (code) obj[key].code = code;
} else if (isString(obj[key])) {
obj[key] = maskString(key, obj[key], props, options);
}
}

return obj;
Expand Down
12 changes: 12 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,15 @@ test('parses ip address', (t) => {
'127.0.0.1'
);
});

test('preserves error code', (t) => {
const err = new Error('oops');
err.code = 1;
const obj = parseRequest({
req: {
body: { err }
}
});
const body = JSON.parse(obj.request.body);
t.is(body.err.code, 1);
});

0 comments on commit bde7c3f

Please sign in to comment.