Skip to content

Commit e702d84

Browse files
authored
Update Angular error regex to work w/ minified exceptions (fixes #606) (#667)
1 parent 11c54f0 commit e702d84

File tree

2 files changed

+88
-16
lines changed

2 files changed

+88
-16
lines changed

plugins/angular.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
'use strict';
77

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

1112
function angularPlugin(Raven, angular) {
1213
angular = angular || window.angular;
@@ -38,23 +39,29 @@ function angularPlugin(Raven, angular) {
3839
.config(['$provide', ExceptionHandlerProvider]);
3940

4041
Raven.setDataCallback(function(data, original) {
41-
// We only care about mutating an exception
42-
var exception = data.exception;
43-
if (exception) {
44-
exception = exception.values[0];
45-
var matches = angularPattern.exec(exception.value);
46-
47-
if (matches) {
48-
// This type now becomes something like: $rootScope:inprog
49-
exception.type = matches[1];
50-
exception.value = matches[2];
51-
data.message = exception.type + ': ' + exception.value;
52-
// auto set a new tag specifically for the angular error url
53-
data.extra.angularDocs = matches[3].substr(0, 250);
54-
}
55-
}
42+
angularPlugin._normalizeData(data);
43+
5644
original && original(data);
5745
});
5846
}
5947

48+
angularPlugin._normalizeData = function (data) {
49+
// We only care about mutating an exception
50+
var exception = data.exception;
51+
if (exception) {
52+
exception = exception.values[0];
53+
var matches = angularPattern.exec(exception.value);
54+
55+
if (matches) {
56+
// This type now becomes something like: $rootScope:inprog
57+
exception.type = matches[1];
58+
exception.value = matches[2];
59+
60+
data.message = exception.type + ': ' + exception.value;
61+
// auto set a new tag specifically for the angular error url
62+
data.extra.angularDocs = matches[3].substr(0, 250);
63+
}
64+
}
65+
};
66+
6067
module.exports = angularPlugin;

test/plugins/angular.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var _Raven = require('../../src/raven');
2+
var angularPlugin = require('../../plugins/angular');
3+
4+
var Raven;
5+
describe('Angular plugin', function () {
6+
beforeEach(function () {
7+
Raven = new _Raven();
8+
Raven.config('http://[email protected]:80/2');
9+
});
10+
11+
describe('_normalizeData()', function () {
12+
it('should extract type, value, and the angularDocs URL from unminified exceptions', function () {
13+
var data = {
14+
project: '2',
15+
logger: 'javascript',
16+
platform: 'javascript',
17+
18+
culprit: 'http://example.org/app.js',
19+
message: 'Error: crap',
20+
exception: {
21+
type: 'Error',
22+
values: [{
23+
value:
24+
'[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' +
25+
'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'
26+
}]
27+
},
28+
extra: {}
29+
};
30+
31+
angularPlugin._normalizeData(data);
32+
33+
var exception = data.exception.values[0];
34+
assert.equal(exception.type, 'ngRepeat:dupes');
35+
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"}');
36+
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');
37+
});
38+
39+
it('should extract type and the angularDocs URL minified exceptions', function () {
40+
var data = {
41+
project: '2',
42+
logger: 'javascript',
43+
platform: 'javascript',
44+
45+
culprit: 'http://example.org/app.js',
46+
message: 'Error: crap',
47+
exception: {
48+
type: 'Error',
49+
values: [{
50+
// no message, no newlines
51+
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'
52+
}]
53+
},
54+
extra: {}
55+
};
56+
57+
angularPlugin._normalizeData(data);
58+
59+
var exception = data.exception.values[0];
60+
assert.equal(exception.type, 'ngRepeat:dupes');
61+
assert.equal(exception.value, '');
62+
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');
63+
});
64+
});
65+
});

0 commit comments

Comments
 (0)