8000 Revise createInstance to eliminate throwing of unexpected exceptions · optimizely/javascript-sdk@c9a1366 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9a1366

Browse files
committed
Revise createInstance to eliminate throwing of unexpected exceptions
1 parent 9069fbf commit c9a1366

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

packages/optimizely-sdk/lib/index.browser.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,30 @@ var createInstance = function(config) {
5959
try {
6060
config = config || {};
6161

62+
try {
63+
configValidator.validate(config);
64+
config.isValidInstance = true;
65+
} catch (ex) {
66+
logger.error(ex);
67+
config.isValidInstance = false;
68+
}
69+
6270
// TODO warn about setting per instance errorHandler / logger / logLevel
63-
if (config.errorHandler) {
71+
if (config.errorHandler && config.isValidInstance) {
6472
setErrorHandler(config.errorHandler);
6573
}
66-
if (config.logger) {
74+
if (config.logger && config.isValidInstance) {
6775
setLogHandler(config.logger);
6876
// respect the logger's shouldLog functionality
6977
setLogLevel(LogLevel.NOTSET);
7078
}
7179
if (config.logLevel !== undefined) {
7280
setLogLevel(config.logLevel);
7381
}
74-
75-
try {
76-
configValidator.validate(config);
77-
config.isValidInstance = true;
78-
} catch (ex) {
79-
logger.error(ex);
80-
config.isValidInstance = false;
81-
}
82-
8382
var eventDispatcher;
83+
if (config.eventDispatcher && config.isValidInstance) {
84+
eventDispatcher = config.eventDispatcher;
85+
}
8486
// prettier-ignore
8587
if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
8688
// only wrap the event dispatcher with pending events retry if the user didnt override
@@ -92,8 +94,6 @@ var createInstance = function(config) {
9294
eventDispatcher.sendPendingEvents();
9395
hasRetriedEvents = true;
9496
}
95-
} else {
96-
eventDispatcher = config.eventDispatcher;
9797
}
9898

9999
config = fns.assign(

packages/optimizely-sdk/lib/index.browser.tests.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import Optimizely from './optimizely';
2222
import testData from './tests/test_data';
2323
import packageJSON from '../package.json';
2424
import optimizelyFactory from './index.browser';
25-
import configValidator from './utils/config_validator';
2625
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
2726

2827
var LocalStoragePendingEventsDispatcher = eventProcessor.LocalStoragePendingEventsDispatcher;
@@ -60,7 +59,6 @@ describe('javascript-sdk', function() {
6059
logToConsole: false,
6160
});
6261
sinon.spy(console, 'error');
63-
sinon.stub(configValidator, 'validate');
6462

6563
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
6664

@@ -71,7 +69,6 @@ describe('javascript-sdk', function() {
7169
LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents.restore();
7270
optimizelyFactory.__internalResetRetryState();
7371
console.error.restore();
74-
configValidator.validate.restore();
7572
delete global.XMLHttpRequest
7673
});
7774

@@ -125,12 +122,33 @@ describe('javascript-sdk', function() {
125122
sinon.assert.calledOnce(LocalStoragePendingEventsDispatcher.prototype.sendPendingEvents);
126123
});
127124

128-
it('should not throw if the provided config is not valid', function() {
129-
configValidator.validate.throws(new Error('Invalid config or something'));
125+
it('should not throw an error if the provided config contains an invalid errorHandler', function() {
130126
assert.doesNotThrow(function() {
131127
var optlyInstance = optimizelyFactory.createInstance({
132128
datafile: {},
133-
logger: silentLogger,
129+
errorHandler: 'invalid errorHandler',
130+
});
131+
// Invalid datafile causes onReady Promise rejection - catch this error
132+
optlyInstance.onReady().catch(function() {});
133+
});
134+
});
135+
136+
it('should not throw an error if the provided config contains an invalid logger', function() {
137+
assert.doesNotThrow(function() {
138+
var optlyInstance = optimizelyFactory.createInstance({
139+
datafile: {},
140+
logger: 'invalid logger',
141+
});
142+
// Invalid datafile causes onReady Promise rejection - catch this error
143+
optlyInstance.onReady().catch(function() {});
144+
});
< 628C /code>
145+
});
146+
147+
it('should not throw an error if the provided config contains an invalid eventDispatcher', function() {
148+
assert.doesNotThrow(function() {
149+
var optlyInstance = optimizelyFactory.createInstance({
150+
datafile: {},
151+
eventDispatcher: 'invalid eventDispatcher',
134152
});
135153
// Invalid datafile causes onReady Promise rejection - catch this error
136154
optlyInstance.onReady().catch(function() {});

0 commit comments

Comments
 (0)
0