Skip to content

Commit ac62a10

Browse files
author
Thomas Grainger
committed
Ensure that Angular and react-native data callbacks delegate to original cb
and that react native calls original.
1 parent a264d8d commit ac62a10

File tree

4 files changed

+87
-10
lines changed

4 files changed

+87
-10
lines changed

plugins/angular.js

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

8+
var wrappedCallback = require('../src/utils').wrappedCallback;
9+
810
// See https://github.com/angular/angular.js/blob/v1.4.7/src/minErr.js
911
var angularPattern = /^\[((?:[$a-zA-Z0-9]+:)?(?:[$a-zA-Z0-9]+))\] (.*?)\n?(\S+)$/;
1012

@@ -38,11 +40,9 @@ function angularPlugin(Raven, angular) {
3840
.provider('Raven', RavenProvider)
3941
.config(['$provide', ExceptionHandlerProvider]);
4042

41-
Raven.setDataCallback(function(data, original) {
42-
angularPlugin._normalizeData(data);
43-
44-
original && original(data);
45-
});
43+
Raven.setDataCallback(wrappedCallback(function(data) {
44+
return angularPlugin._normalizeData(data);
45+
}));
4646
}
4747

4848
angularPlugin._normalizeData = function (data) {
@@ -62,6 +62,8 @@ angularPlugin._normalizeData = function (data) {
6262
data.extra.angularDocs = matches[3].substr(0, 250);
6363
}
6464
}
65+
66+
return data;
6567
};
6668

6769
module.exports = angularPlugin;

plugins/react-native.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
'use strict';
2121

22+
var wrappedCallback = require('../src/utils').wrappedCallback;
23+
2224
// Example React Native path format (iOS):
2325
// /var/containers/Bundle/Application/{DEVICE_ID}/HelloWorld.app/main.jsbundle
2426

@@ -60,9 +62,9 @@ function reactNativePlugin(Raven, options) {
6062
Raven.setTransport(reactNativePlugin._transport);
6163

6264
// Use data callback to strip device-specific paths from stack traces
63-
Raven.setDataCallback(function(data) {
64-
reactNativePlugin._normalizeData(data, options.pathStrip)
65-
});
65+
Raven.setDataCallback(wrappedCallback(function(data) {
66+
return reactNativePlugin._normalizeData(data, options.pathStrip);
67+
}));
6668

6769
// Check for a previously persisted payload, and report it.
6870
reactNativePlugin._restorePayload()
@@ -224,6 +226,7 @@ reactNativePlugin._normalizeData = function (data, pathStripRe) {
224226
frame.filename = normalizeUrl(frame.filename, pathStripRe);
225227
});
226228
}
229+
return data;
227230
};
228231

229232
module.exports = reactNativePlugin;

src/utils.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,20 @@ function isError(what) {
1414
what instanceof Error;
1515
}
1616

17+
function wrappedCallback(callback) {
18+
function dataCallback(data, original) {
19+
var normalizedData = callback(data) || data;
20+
if (original) {
21+
return original(normalizedData) || normalizedData;
22+
}
23+
return normalizedData;
24+
}
25+
26+
return dataCallback;
27+
}
28+
1729
module.exports = {
1830
isObject: isObject,
19-
isError: isError
20-
};
31+
isError: isError,
32+
wrappedCallback: wrappedCallback
33+
};

test/utils.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'use strict';
44

55
var Raven = require('../src/raven');
6+
var wrappedCallback = require('../src/utils').wrappedCallback;
67
var utils = Raven.utils;
78
var RavenConfigError = require('../src/configError');
89

@@ -262,4 +263,62 @@ describe('utils', function () {
262263
});
263264
});
264265
});
266+
267+
describe('wrappedCallback', function () {
268+
it('should return data from callback', function () {
269+
var expected = 'yup';
270+
var cb = wrappedCallback(function() {
271+
return expected;
272+
});
273+
assert.equal(cb(), expected);
274+
});
275+
276+
it('should return mutated data from callback', function () {
277+
var cb = wrappedCallback(function(data) {
278+
data.mutated = true;
279+
});
280+
assert.deepEqual(cb({}), {mutated: true});
281+
});
282+
283+
it('should return data from original', function () {
284+
var expected = 'yup';
285+
var cb = wrappedCallback(function(data) {
286+
return 'nope';
287+
});
288+
289+
function original() {
290+
return expected;
291+
}
292+
assert.equal(cb({}, original), expected);
293+
});
294+
295+
it('should return mutated data from original', function () {
296+
var cb = wrappedCallback(function(data) {
297+
data.mutatedSomeMore = true;
298+
});
299+
300+
function original(data) {
301+
data.mutated = true;
302+
}
303+
assert.deepEqual(cb({}, original), {
304+
mutated: true,
305+
mutatedSomeMore: true
306+
});
307+
});
308+
309+
it('should call callback and original in the right order', function () {
310+
var cb = wrappedCallback(function(data) {
311+
return data + 'callback first, ';
312+
});
313+
314+
function original(data) {
315+
return data + 'then the original.';
316+
}
317+
318+
assert.equal(
319+
cb('it will run the ', original),
320+
'it will run the callback first, then the original.'
321+
);
322+
});
323+
});
265324
});

0 commit comments

Comments
 (0)