Skip to content

Commit d2ae868

Browse files
ajhorstAJ Horst
andauthored
fix: call onError in init function (#368)
* AMP-32671 read in functions from options arg to amplitude client options * AMP-32671 call onError from args in amplitude client initialization - the client's options may not have been initialized when an error occurs, so call onError from the args to make sure it's invoked regardless of when the error is thrown * AMP-32571 add unit test for onError Co-authored-by: AJ Horst <[email protected]>
1 parent 87b42c1 commit d2ae868

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/amplitude-client.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
275275
}
276276
} catch (err) {
277277
utils.log.error(err);
278-
this.options.onError(err);
278+
if (type(opt_config.onError) === 'function') {
279+
opt_config.onError(err);
280+
}
279281
}
280282
};
281283

@@ -444,7 +446,8 @@ var _parseConfig = function _parseConfig(options, config) {
444446
options[key] = !!inputValue;
445447
} else if (
446448
(expectedType === 'string' && !utils.isEmptyString(inputValue)) ||
447-
(expectedType === 'number' && inputValue > 0)
449+
(expectedType === 'number' && inputValue > 0) ||
450+
expectedType === 'function'
448451
) {
449452
options[key] = inputValue;
450453
} else if (expectedType === 'object') {

test/amplitude-client.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,17 @@ describe('AmplitudeClient', function () {
824824
},
825825
});
826826
});
827+
828+
it('should call the supplied onError function if an error occurs during init()', () => {
829+
var onErrorSpy = sinon.spy();
830+
831+
var amplitude = new AmplitudeClient();
832+
sinon.stub(amplitude.cookieStorage, 'options').throws();
833+
amplitude.init(apiKey, null, { onError: onErrorSpy });
834+
assert.isTrue(onErrorSpy.calledOnce);
835+
836+
amplitude.cookieStorage.options.restore();
837+
});
827838
});
828839

829840
describe('runQueuedFunctions', function () {

0 commit comments

Comments
 (0)