diff --git a/src/Errors.js b/src/Errors.js index 22cf0ce..6f92075 100644 --- a/src/Errors.js +++ b/src/Errors.js @@ -38,9 +38,9 @@ function toRapidResponse(error) { try { if (util.types.isNativeError(error) || _isError(error)) { return { - errorType: error.name, - errorMessage: error.message, - trace: error.stack.split('\n'), + errorType: error.name?.replace(/\x7F/g, '%7F'), + errorMessage: error.message?.replace(/\x7F/g, '%7F'), + trace: error.stack.replace(/\x7F/g, '%7F').split('\n'), }; } else { return { diff --git a/src/XRayError.js b/src/XRayError.js index 86c01d1..520152e 100644 --- a/src/XRayError.js +++ b/src/XRayError.js @@ -50,7 +50,7 @@ class XRayFormattedCause { let stack = []; if (err.stack) { - let stackLines = err.stack.split('\n'); + let stackLines = err.stack.replace(/\x7F/g, '%7F').split('\n'); stackLines.shift(); stackLines.forEach((stackLine) => { @@ -79,8 +79,8 @@ class XRayFormattedCause { this.exceptions = [ { - type: err.name, - message: err.message, + type: err.name?.replace(/\x7F/g, '%7F'), + message: err.message?.replace(/\x7F/g, '%7F'), stack: stack, }, ]; diff --git a/test/unit/ErrorsTest.js b/test/unit/ErrorsTest.js index f72b73b..9c0f9a5 100644 --- a/test/unit/ErrorsTest.js +++ b/test/unit/ErrorsTest.js @@ -19,3 +19,14 @@ describe('Formatted Error Logging', () => { loggedError.should.have.property('trace').with.length(11); }); }); + +describe('Invalid chars in HTTP header', () => { + it('should be replaced', () => { + let errorWithInvalidChar = new Error('\x7F \x7F'); + errorWithInvalidChar.name = 'ErrorWithInvalidChar'; + + let loggedError = Errors.toRapidResponse(errorWithInvalidChar); + loggedError.should.have.property('errorType', 'ErrorWithInvalidChar'); + loggedError.should.have.property('errorMessage', '%7F %7F'); + }); +});