Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 10 additions & 2 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ Optimizely.prototype._sendImpressionEvent = function(experimentKey, variationKey
impressionEvent.url,
JSON.stringify(impressionEvent.params));
this.logger.log(LOG_LEVEL.DEBUG, dispatchedImpressionEventLogMessage);
var eventDispatcherCallback = function() {
var eventDispatcherCallback = function(err, response) {
if (err) {
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.FAILED_TO_TRACK_IMPRESSION_EVENT, MODULE_NAME, experimentKey, err));
return;
}
var activatedLogMessage = sprintf(LOG_MESSAGES.ACTIVATE_USER, MODULE_NAME, userId, experimentKey);
this.logger.log(LOG_LEVEL.INFO, activatedLogMessage);
}.bind(this);
Expand Down Expand Up @@ -263,7 +267,11 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
JSON.stringify(conversionEvent.params));
this.logger.log(LOG_LEVEL.DEBUG, dispatchedConversionEventLogMessage);

var eventDispatcherCallback = function() {
var eventDispatcherCallback = function(err) {
if (err) {
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.FAILED_TO_TRACK_CONVERSION_EVENT, MODULE_NAME, eventKey, err));
return;
}
var trackedLogMessage = sprintf(LOG_MESSAGES.TRACK_EVENT, MODULE_NAME, eventKey, userId);
this.logger.log(LOG_LEVEL.INFO, trackedLogMessage);
}.bind(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
req.setRequestHeader('Content-Type', 'application/json');
req.onreadystatechange = function() {
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
callback(params);
callback(null, params);
}
};
req.send(JSON.stringify(params));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ module.exports = {
}
};

var requestCallback = function(error, response, body) {
if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400 && callback && typeof callback === 'function') {
callback();
var requestCallback = function(response) {
if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
callback(null, response);
}
};

var req = (parsedUrl.protocol === 'http:' ? http : https).request(requestOptions, requestCallback);
req.on('error', callback);
req.write(dataString);
req.end();
return req;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
httpVerb: 'POST',
};

eventDispatcher.dispatchEvent(eventObj)
.on('response', function(response) {
assert.isTrue(response.statusCode === 200);
done();
})
.on('error', function(error) {
assert.fail('status code okay', 'status code not okay', '');
});
eventDispatcher.dispatchEvent(eventObj, done);
});

it('should execute the callback passed to event dispatcher', function(done) {
Expand Down Expand Up @@ -98,5 +91,18 @@ describe('lib/plugins/event_dispatcher/node', function() {
eventDispatcher.dispatchEvent(eventObj, callback);
});
});

it('calls callback with err when one is emitted', function(done) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: before changing the event_dispatcher code, the .dispatchEvent call did indeed throw an error.

var eventObj = {
url: 'https://example',
params: {},
httpVerb: 'POST',
};

eventDispatcher.dispatchEvent(eventObj, function(err) {
assert.isNotNull(err);
done();
});
});
});
});
2 changes: 2 additions & 0 deletions packages/optimizely-sdk/lib/utils/enums/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ exports.LOG_MESSAGES = {
FEATURE_HAS_NO_EXPERIMENTS: '%s: Feature %s is not attached to any experiments.',
FAILED_TO_PARSE_VALUE: '%s: Failed to parse event value "%s" from event tags.',
FAILED_TO_PARSE_REVENUE: '%s: Failed to parse revenue value "%s" from event tags.',
FAILED_TO_TRACK_CONVERSION_EVENT: '%s: Failed to track conversion event for %s: %s',
FAILED_TO_TRACK_IMPRESSION_EVENT: '%s: Failed to track impression event for experiment %s: %s',
FORCED_BUCKETING_FAILED: '%s: Variation key %s is not in datafile. Not activating user %s.',
INVALID_OBJECT: '%s: Optimizely object is not valid. Failing %s.',
INVALID_CLIENT_ENGINE: '%s: Invalid client engine passed: %s. Defaulting to node-sdk.',
Expand Down