Skip to content

Prevent truncate method to throw an error when crumb.data.url is undefined #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ Raven.prototype = {
data = objectMerge({}, crumb.data);
for (var j = 0; j < urlProps.length; ++j) {
urlProp = urlProps[j];
if (data.hasOwnProperty(urlProp)) {
if (data.hasOwnProperty(urlProp) && data[urlProp]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only problem with this is that it will skip empty strings. It's probably safer to explicitly check for undefined. Or alternatively, change truncate to not explode on undefined values.

Copy link
Contributor Author

@gregorylegarec gregorylegarec Aug 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first thought was actually to update truncate. I may amend my commit.

By the way, an empty string does not need to be truncated, and testing falsy values also handle null. So let me know what your preference is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, an empty string does not need to be truncated

Good point. Okay, I mean given that there are tests, we can proceed as-is.

data[urlProp] = truncate(data[urlProp], this._globalOptions.maxUrlLength);
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,31 @@ describe('globals', function() {
});
});

it('should not throw error when url in breadcrumb is undefined', function() {
this.sinon.stub(Raven, 'isSetup').returns(true);
this.sinon.stub(Raven, '_makeRequest');
this.sinon.stub(Raven, '_getHttpData').returns({
url: 'http://localhost/?a=b',
headers: {'User-Agent': 'lolbrowser'}
});

Raven._globalProject = '2';
Raven._globalOptions = {
logger: 'javascript',
maxMessageLength: 100
};
Raven._globalOptions.maxUrlLength = 35;

var obj = {method: 'POST', url: undefined};
// Do not freeze crumb data

Raven._breadcrumbs = [{type: 'request', timestamp: 0.1, data: obj}];

assert.doesNotThrow(function() {
Raven._send({message: 'bar'});
}, TypeError)
});

});

describe('makeRequest', function() {
Expand Down