Skip to content

Commit dbe7280

Browse files
committed
refactor(logging): Update ReportingLogger initialization and enhance error logging in RoktManager for improved clarity
1 parent 4fea894 commit dbe7280

File tree

5 files changed

+29
-12
lines changed

5 files changed

+29
-12
lines changed

src/mp-instance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ function createIdentityCache(mpInstance) {
15531553
}
15541554

15551555
function runPreConfigFetchInitialization(mpInstance, apiKey, config) {
1556-
const reportingLogger = new ReportingLogger(Constants.sdkVersion, mpInstance);
1556+
const reportingLogger = new ReportingLogger(mpInstance, Constants.sdkVersion);
15571557
mpInstance.Logger = new Logger(config, reportingLogger);
15581558
mpInstance._Store = new Store(config, mpInstance, apiKey);
15591559
window.mParticle.Store = mpInstance._Store;

src/mparticle-instance-manager.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import MPSideloadedKit from './sideloadedKit';
77
import { IMParticleInstanceManager } from './sdkRuntimeModels';
88
import { IStore } from './store';
99
import { Dictionary } from '@mparticle/web-sdk';
10+
import { IReportingLogger } from './logging/reportingLogger';
1011

1112
if (!Array.prototype.forEach) {
1213
Array.prototype.forEach = Polyfill.forEach;
@@ -446,18 +447,20 @@ function mParticleInstanceManager(this: IMParticleInstanceManager) {
446447
self.getInstance().reset(self.getInstance());
447448
};
448449

449-
this._resetForTests = function(MPConfig, keepPersistence) {
450+
this._resetForTests = function(MPConfig, keepPersistence, _?: IMParticleWebSDKInstance, reportingLogger?: IReportingLogger) {
450451
if (typeof keepPersistence === 'boolean') {
451452
self.getInstance()._resetForTests(
452453
MPConfig,
453454
keepPersistence,
454-
self.getInstance()
455+
self.getInstance(),
456+
reportingLogger
455457
);
456458
} else {
457459
self.getInstance()._resetForTests(
458460
MPConfig,
459461
false,
460-
self.getInstance()
462+
self.getInstance(),
463+
reportingLogger
461464
);
462465
}
463466
};

src/roktManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export default class RoktManager {
346346

347347
this.messageQueue.forEach((message) => {
348348
if(!(message.methodName in this) || !isFunction(this[message.methodName])) {
349-
this.logger?.error(`RoktManager: Method ${message.methodName} not found`);
349+
this.logger?.error(`RoktManager: Method ${message.methodName} not found`, ErrorCodes.ROKT_MANAGER);
350350

351351
return;
352352
}

test/jest/roktManager.spec.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { IKitConfigs } from "../../src/configAPIClient";
22
import { IMParticleUser } from "../../src/identity-user-interfaces";
33
import { SDKIdentityApi } from "../../src/identity.interfaces";
4+
import { ErrorCodes } from "../../src/logging/errorCodes";
45
import { IMParticleWebSDKInstance } from "../../src/mp-instance";
56
import RoktManager, { IRoktKit, IRoktSelectPlacementsOptions } from "../../src/roktManager";
67
import { testMPID } from '../src/config/constants';
@@ -554,7 +555,8 @@ describe('RoktManager', () => {
554555

555556
// Verify error was logged for non-existent method
556557
expect(mockMPInstance.Logger.error).toHaveBeenCalledWith(
557-
'RoktManager: Method nonExistentMethod not found'
558+
'RoktManager: Method nonExistentMethod not found',
559+
ErrorCodes.ROKT_MANAGER
558560
);
559561

560562
// Verify message was removed from queue even though method didn't exist
@@ -1030,8 +1032,10 @@ describe('RoktManager', () => {
10301032
}
10311033
}, expect.any(Function));
10321034
expect(mockMPInstance.Logger.warning).toHaveBeenCalledWith(
1033-
'Email mismatch detected. Current email differs from email passed to selectPlacements call. Proceeding to call identify with email from selectPlacements call. Please verify your implementation.'
1035+
'Email mismatch detected. Current email differs from email passed to selectPlacements call. Proceeding to call identify with email from selectPlacements call. Please verify your implementation.',
1036+
ErrorCodes.EMAIL_MISMATCH
10341037
);
1038+
expect(mockMPInstance.Logger.error).not.toHaveBeenCalled();
10351039
});
10361040

10371041
it('should not call identify when email matches current user email', () => {
@@ -1217,7 +1221,8 @@ describe('RoktManager', () => {
12171221
}
12181222
}, expect.any(Function));
12191223
expect(mockMPInstance.Logger.warning).toHaveBeenCalledWith(
1220-
"emailsha256 mismatch detected. Current mParticle hashedEmail differs from hashedEmail passed to selectPlacements call. Proceeding to call identify with hashedEmail from selectPlacements call. Please verify your implementation."
1224+
"emailsha256 mismatch detected. Current mParticle hashedEmail differs from hashedEmail passed to selectPlacements call. Proceeding to call identify with hashedEmail from selectPlacements call. Please verify your implementation.",
1225+
ErrorCodes.EMAILSHA256_MISMATCH
12211226
);
12221227
});
12231228

@@ -1307,7 +1312,8 @@ describe('RoktManager', () => {
13071312
}
13081313
}, expect.any(Function));
13091314
expect(mockMPInstance.Logger.warning).toHaveBeenCalledWith(
1310-
"emailsha256 mismatch detected. Current mParticle hashedEmail differs from hashedEmail passed to selectPlacements call. Proceeding to call identify with hashedEmail from selectPlacements call. Please verify your implementation."
1315+
"emailsha256 mismatch detected. Current mParticle hashedEmail differs from hashedEmail passed to selectPlacements call. Proceeding to call identify with hashedEmail from selectPlacements call. Please verify your implementation.",
1316+
ErrorCodes.EMAILSHA256_MISMATCH
13111317
);
13121318
});
13131319

@@ -1396,7 +1402,8 @@ describe('RoktManager', () => {
13961402

13971403
// Verify error was logged
13981404
expect(mockMPInstance.Logger.error).toHaveBeenCalledWith(
1399-
'Failed to identify user with new email: ' + JSON.stringify(mockError)
1405+
'Failed to identify user with new email: ' + JSON.stringify(mockError),
1406+
ErrorCodes.ROKT_MANAGER
14001407
);
14011408

14021409
// Verify selectPlacements was still called

test/src/tests-eCommerce.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ import fetchMock from 'fetch-mock/esm/client';
44
import { urls, apiKey, MPConfig, testMPID, ProductActionType, PromotionActionType } from './config/constants';
55
const { waitForCondition, fetchMockSuccess, hasIdentifyReturned } = Utils;
66

7+
const mockReportingLogger = {
8+
error: sinon.spy(),
9+
warning: sinon.spy()
10+
};
11+
712
const forwarderDefaultConfiguration = Utils.forwarderDefaultConfiguration,
813
findEventFromRequest = Utils.findEventFromRequest,
914
MockForwarder = Utils.MockForwarder;
1015

1116
describe('eCommerce', function() {
1217
beforeEach(function() {
13-
mParticle._resetForTests(MPConfig);
18+
mockReportingLogger.error.resetHistory();
19+
mockReportingLogger.warning.resetHistory();
20+
mParticle._resetForTests(MPConfig, false, undefined, mockReportingLogger);
1421
delete mParticle._instances['default_instance'];
1522
fetchMock.config.overwriteRoutes = true;
1623
fetchMock.post(urls.events, 200);
@@ -1343,7 +1350,7 @@ describe('eCommerce', function() {
13431350

13441351
it('should deprecate add', async () => {
13451352
await waitForCondition(hasIdentifyReturned);
1346-
mParticle._resetForTests(MPConfig);
1353+
mParticle._resetForTests(MPConfig, false, undefined, mockReportingLogger);
13471354
const bond = sinon.spy(mParticle.getInstance().Logger, 'warning');
13481355

13491356
const product = mParticle.eCommerce.createProduct(

0 commit comments

Comments
 (0)