66 DEEP_HEADER_SET_COOKIE ,
77 REWRITE_REQUEST ,
88 REWRITE_RESPONSE ,
9+ DEEP_HEADER_COOKIE ,
910} = require ( './matchers' )
1011
1112const {
@@ -14,6 +15,8 @@ const {
1415 redactMatchers,
1516} = require ( './utils' )
1617
18+ const { serializeError } = require ( './error' )
19+
1720const { deepMap } = require ( './deep-map' )
1821
1922const _redact = redactMatchers (
@@ -22,6 +25,7 @@ const _redact = redactMatchers(
2225 JSON_WEB_TOKEN ,
2326 DEEP_HEADER_AUTHORIZATION ,
2427 DEEP_HEADER_SET_COOKIE ,
28+ DEEP_HEADER_COOKIE ,
2529 REWRITE_REQUEST ,
2630 REWRITE_RESPONSE ,
2731 redactUrlMatcher (
@@ -31,4 +35,25 @@ const _redact = redactMatchers(
3135
3236const redact = ( input ) => deepMap ( input , ( value , path ) => _redact ( value , { path } ) )
3337
34- module . exports = { redact }
38+ /** takes an error returns new error keeping some custom properties */
39+ function redactError ( input ) {
40+ const { message, ...data } = serializeError ( input )
41+ const output = new Error ( redact ( message ) )
42+ return Object . assign ( output , redact ( data ) )
43+ }
44+
45+ /** runs a function within try / catch and throws error wrapped in redactError */
46+ function redactThrow ( func ) {
47+ if ( typeof func !== 'function' ) {
48+ throw new Error ( 'redactThrow expects a function' )
49+ }
50+ return async ( ...args ) => {
51+ try {
52+ return await func ( ...args )
53+ } catch ( error ) {
54+ throw redactError ( error )
55+ }
56+ }
57+ }
58+
59+ module . exports = { redact, redactError, redactThrow }
0 commit comments