Skip to content

Update Angular error regex to work w/ minified exceptions (fixes #606) #667

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 1 commit into from
Jul 28, 2016
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
39 changes: 23 additions & 16 deletions plugins/angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
'use strict';

// See https://github.com/angular/angular.js/blob/v1.4.7/src/minErr.js
var angularPattern = /^\[((?:[$a-zA-Z0-9]+:)?(?:[$a-zA-Z0-9]+))\] (.+?)\n(\S+)$/;
var angularPattern = /^\[((?:[$a-zA-Z0-9]+:)?(?:[$a-zA-Z0-9]+))\] (.*?)\n?(\S+)$/;
Copy link
Contributor

Choose a reason for hiding this comment

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

? is the only meaningful change here, isn't it? Changes in data callback are only refactoring

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's also a change to the 2nd match group, from (.+?) to (.*?).

Changes in data callback are only refactoring

Yeah that's just so I could easily write a test to verify the regexes work.



function angularPlugin(Raven, angular) {
angular = angular || window.angular;
Expand Down Expand Up @@ -38,23 +39,29 @@ function angularPlugin(Raven, angular) {
.config(['$provide', ExceptionHandlerProvider]);

Raven.setDataCallback(function(data, original) {
// We only care about mutating an exception
var exception = data.exception;
if (exception) {
exception = exception.values[0];
var matches = angularPattern.exec(exception.value);

if (matches) {
// This type now becomes something like: $rootScope:inprog
exception.type = matches[1];
exception.value = matches[2];
data.message = exception.type + ': ' + exception.value;
// auto set a new tag specifically for the angular error url
data.extra.angularDocs = matches[3].substr(0, 250);
}
}
angularPlugin._normalizeData(data);

original && original(data);
});
}

angularPlugin._normalizeData = function (data) {
// We only care about mutating an exception
var exception = data.exception;
if (exception) {
exception = exception.values[0];
var matches = angularPattern.exec(exception.value);

if (matches) {
// This type now becomes something like: $rootScope:inprog
exception.type = matches[1];
exception.value = matches[2];

data.message = exception.type + ': ' + exception.value;
// auto set a new tag specifically for the angular error url
data.extra.angularDocs = matches[3].substr(0, 250);
}
}
};

module.exports = angularPlugin;
65 changes: 65 additions & 0 deletions test/plugins/angular.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var _Raven = require('../../src/raven');
var angularPlugin = require('../../plugins/angular');

var Raven;
describe('Angular plugin', function () {
beforeEach(function () {
Raven = new _Raven();
Raven.config('http://abc@example.com:80/2');
});

describe('_normalizeData()', function () {
it('should extract type, value, and the angularDocs URL from unminified exceptions', function () {
var data = {
project: '2',
logger: 'javascript',
platform: 'javascript',

culprit: 'http://example.org/app.js',
message: 'Error: crap',
exception: {
type: 'Error',
values: [{
value:
'[ngRepeat:dupes] Duplicates in a repeater are not allowed. Use \'track by\' expression to specify unique keys. Repeater: element in elements | orderBy: \'createdAt\' track by element.id, Duplicate key: 25, Duplicate value: {"id":"booking-40"}\n' +
'http://errors.angularjs.org/1.4.3/ngRepeat/dupes?p0=element%20in%20elements…00%3A00%22%2C%22ends_at%22%3A%222016-06-09T23%3A59%3A59%2B00%3A00%22%7D%7D'
}]
},
extra: {}
};

angularPlugin._normalizeData(data);

var exception = data.exception.values[0];
assert.equal(exception.type, 'ngRepeat:dupes');
assert.equal(exception.value, 'Duplicates in a repeater are not allowed. Use \'track by\' expression to specify unique keys. Repeater: element in elements | orderBy: \'createdAt\' track by element.id, Duplicate key: 25, Duplicate value: {"id":"booking-40"}');
assert.equal(data.extra.angularDocs, 'http://errors.angularjs.org/1.4.3/ngRepeat/dupes?p0=element%20in%20elements…00%3A00%22%2C%22ends_at%22%3A%222016-06-09T23%3A59%3A59%2B00%3A00%22%7D%7D');
});

it('should extract type and the angularDocs URL minified exceptions', function () {
var data = {
project: '2',
logger: 'javascript',
platform: 'javascript',

culprit: 'http://example.org/app.js',
message: 'Error: crap',
exception: {
type: 'Error',
values: [{
// no message, no newlines
value: '[ngRepeat:dupes] http://errors.angularjs.org/1.4.3/ngRepeat/dupes?p0=element%20in%20elements…00%3A00%22%2C%22ends_at%22%3A%222016-06-09T23%3A59%3A59%2B00%3A00%22%7D%7D'
}]
},
extra: {}
};

angularPlugin._normalizeData(data);

var exception = data.exception.values[0];
assert.equal(exception.type, 'ngRepeat:dupes');
assert.equal(exception.value, '');
assert.equal(data.extra.angularDocs, 'http://errors.angularjs.org/1.4.3/ngRepeat/dupes?p0=element%20in%20elements…00%3A00%22%2C%22ends_at%22%3A%222016-06-09T23%3A59%3A59%2B00%3A00%22%7D%7D');
});
});
});