Skip to content

Commit

Permalink
Handle invalid char when sending HTTP request to Runtime API (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
andclt authored Feb 15, 2024
1 parent eeac0b3 commit 2ce8861
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/XRayError.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
},
];
Expand Down
11 changes: 11 additions & 0 deletions test/unit/ErrorsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit 2ce8861

Please sign in to comment.