Skip to content
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

Adjust wrapSubscription to handle invalid errors #6

Merged
merged 5 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 19 additions & 17 deletions lib/hijack/wrap_subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,30 @@ wrapSubscription = function(subscriptionProto) {

var originalError = subscriptionProto.error;
subscriptionProto.error = function(err) {
var kadiraInfo = Kadira._getInfo();
if (err) {
var kadiraInfo = Kadira._getInfo();

if(kadiraInfo && this._subscriptionId == kadiraInfo.trace.id) {
Kadira.tracer.endLastEvent(kadiraInfo.trace);
if(kadiraInfo && this._subscriptionId == kadiraInfo.trace.id) {
Kadira.tracer.endLastEvent(kadiraInfo.trace);

var errorForApm = _.pick(err, 'message', 'stack');
Kadira.tracer.event(kadiraInfo.trace, 'error', {error: errorForApm});
var trace = Kadira.tracer.buildTrace(kadiraInfo.trace);
var errorForApm = _.pick(err, 'message', 'stack');
Kadira.tracer.event(kadiraInfo.trace, 'error', {error: errorForApm});
var trace = Kadira.tracer.buildTrace(kadiraInfo.trace);

Kadira.models.pubsub._trackError(this._session, this, trace);
Kadira.models.pubsub._trackError(this._session, this, trace);

// error tracking can be disabled and if there is a trace
// trace should be avaialble all the time, but it won't
// if something wrong happened on the trace building
if(Kadira.options.enableErrorTracking && trace) {
Kadira.models.error.trackError(err, trace);
// error tracking can be disabled and if there is a trace
// trace should be avaialble all the time, but it won't
// if something wrong happened on the trace building
if(Kadira.options.enableErrorTracking && trace) {
Kadira.models.error.trackError(err, trace);
}
}
}

// wrap error stack so Meteor._debug can identify and ignore it
err.stack = {stack: err.stack, source: 'subscription'};
originalError.call(this, err);
// wrap error stack so Meteor._debug can identify and ignore it
err.stack = {stack: err.stack, source: 'subscription'};
originalError.call(this, err);
}
};

var originalDeactivate = subscriptionProto._deactivate;
Expand All @@ -91,4 +93,4 @@ wrapSubscription = function(subscriptionProto) {
return res;
};
});
};
};
5 changes: 3 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
"summary": "Performance Monitoring for Meteor",
"version": "3.1.1",
"version": "3.1.2",
"git": "https://github.com/meteor/meteor-apm-agent.git",
"name": "mdg:meteor-apm-agent"
});
Expand All @@ -12,7 +12,8 @@ var npmModules = {
"evloop-monitor": "0.1.0",
"pidusage": "0.1.1",
"lru-cache": "4.0.0",
"json-stringify-safe": "5.0.1"
"json-stringify-safe": "5.0.1",
"uuid": "3.3.2"
};

Npm.depends(npmModules);
Expand Down
9 changes: 5 additions & 4 deletions tests/client/error_reporters/meteor_debug.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
var uuid = Npm.require('uuid');

Tinytest.add(
'Client Side - Error Manager - Reporters - meteor._debug - with zone',
TestWithErrorTracking(function (test) {
hijackKadiraSendErrors(mock_KadiraSendErrors);
test.equal(typeof Meteor._debug, 'function');
var errorSent = false;
var message = Meteor.uuid();
var message = uuid();

// set window.zone as nothing
var originalZone = window.zone;
Expand All @@ -15,7 +16,7 @@ Tinytest.add(
test.equal(errorSent, false);
restoreKadiraSendErrors();

// cleajr
// cleajr
window.zone = originalZone;
function mock_KadiraSendErrors(data) {
errorSent = true;
Expand All @@ -30,7 +31,7 @@ Tinytest.add(
test.equal(typeof Meteor._debug, 'function');
var errorSent = false;
var originalZone = window.zone;
var message = Meteor.uuid();
var message = uuid();
window.zone = undefined;

try {
Expand Down Expand Up @@ -61,7 +62,7 @@ Tinytest.add(
test.equal(typeof Meteor._debug, 'function');
var errorSent = false;
var originalZone = window.zone;
var message = Meteor.uuid();
var message = uuid();
window.zone = undefined;

try {
Expand Down
5 changes: 3 additions & 2 deletions tests/client/error_reporters/window_error.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
var uuid = Npm.require('uuid');

Tinytest.addAsync(
'Client Side - Error Manager - Reporters - window.onerror - with all args',
TestWithErrorTrackingAsync(function (test, next) {
hijackKadiraSendErrors(mock_KadiraSendErrors);
test.equal(typeof window.onerror, 'function');
var error = new Error('test-error');
var message = Meteor.uuid();
var message = uuid();
window.onerror(message, '_url', 1, 1, error);

function mock_KadiraSendErrors(error) {
Expand All @@ -27,7 +28,7 @@ Tinytest.addAsync(
TestWithErrorTrackingAsync(function (test, next) {
hijackKadiraSendErrors(mock_KadiraSendErrors);
test.equal(typeof window.onerror, 'function');
var message = Meteor.uuid();
var message = uuid();
window.onerror(message, '_url', 1, 1);

function mock_KadiraSendErrors(error) {
Expand Down
41 changes: 41 additions & 0 deletions tests/hijack/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,44 @@ Tinytest.add(
CloseClient(client);
}
);

Tinytest.add(
'Subscriptions - wrapSubscription - handle undefined errors',
function (test) {
var subscription = {
error: function (error) {
// Do nothing
}
};

wrapSubscription(subscription);

// Valid error should work
var error1 = new Error('Oh no!');
test.isUndefined(error1.stack.source);
subscription.error(error1);
test.isNotUndefined(error1.stack.source);

// Invalid error should be ignored
var getInfo = Kadira._getInfo;
Kadira._getInfo = function () {
return {
trace: {
id: 'abc123',
events: [{
key: 'value',
}],
},
};
};

subscription._subscriptionId = 'abc123';
try {
subscription.error(undefined);
} catch (error) {
test.fail('Invalid errors should not throw an exception');
} finally {
Kadira._getInfo = getInfo;
}
}
);