Skip to content

Commit 3814f26

Browse files
committed
refactor(logging): Integrate ErrorCodes into logging methods across multiple modules for improved error handling
1 parent cb0222e commit 3814f26

20 files changed

+288
-110
lines changed

src/apiClient.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AsyncUploader, FetchUploader, XHRUploader } from './uploaders';
1111
import { IMParticleWebSDKInstance } from './mp-instance';
1212
import { appendUserInfo } from './user-utils';
1313
import { LogRequest } from './logging/logRequest';
14+
import { ErrorCodes } from './logging/errorCodes';
1415

1516
export interface IAPIClient {
1617
uploader: BatchUploader | null;
@@ -174,7 +175,8 @@ export default function APIClient(
174175
}
175176
} catch (e) {
176177
mpInstance.Logger.error(
177-
'Error sending forwarding stats to mParticle servers.'
178+
'Error sending forwarding stats to mParticle servers.',
179+
ErrorCodes.API_CLIENT_ERROR
178180
);
179181
}
180182
};

src/audienceManager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
IFetchPayload
77
} from './uploaders';
88
import Audience from './audience';
9+
import { ErrorCodes } from './logging/errorCodes';
910

1011
export interface IAudienceMembershipsServerResponse {
1112
dt: 'cam'; // current audience memberships
@@ -81,7 +82,8 @@ export default class AudienceManager {
8182
}
8283
} catch (e) {
8384
this.logger.error(
84-
`Error retrieving audiences. ${e}`
85+
`Error retrieving audiences. ${e}`,
86+
ErrorCodes.AUDIENCE_MANAGER_ERROR
8587
);
8688
}
8789
}

src/batchUploader.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { IMParticleUser } from './identity-user-interfaces';
1515
import { IMParticleWebSDKInstance } from './mp-instance';
1616
import { appendUserInfo } from './user-utils';
1717
import { IntegrationAttributes } from './store';
18+
import { ErrorCodes } from './logging/errorCodes';
1819
/**
1920
* BatchUploader contains all the logic to store/retrieve events and batches
2021
* to/from persistence, and upload batches to mParticle.
@@ -489,13 +490,15 @@ export class BatchUploader {
489490
response.status === 429
490491
) {
491492
logger.error(
492-
`HTTP error status ${response.status} received`
493+
`HTTP error status ${response.status} received`,
494+
ErrorCodes.BATCH_UPLOADER_ERROR
493495
);
494496
// Server error, add back current batches and try again later
495497
return uploads.slice(i, uploads.length);
496498
} else if (response.status >= 401) {
497499
logger.error(
498-
`HTTP error status ${response.status} while uploading - please verify your API key.`
500+
`HTTP error status ${response.status} while uploading - please verify your API key.`,
501+
ErrorCodes.BATCH_UPLOADER_ERROR
499502
);
500503
//if we're getting a 401, assume we'll keep getting a 401 and clear the uploads.
501504
return null;
@@ -512,7 +515,8 @@ export class BatchUploader {
512515
}
513516
} catch (e) {
514517
logger.error(
515-
`Error sending event to mParticle servers. ${e}`
518+
`Error sending event to mParticle servers. ${e}`,
519+
ErrorCodes.BATCH_UPLOADER_ERROR
516520
);
517521
return uploads.slice(i, uploads.length);
518522
}

src/consent.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import KitFilterHelper from './kitFilterHelper';
99
import Constants from './constants';
1010
import { IMParticleUser } from './identity-user-interfaces';
1111
import { IMParticleWebSDKInstance } from './mp-instance';
12+
import { ErrorCodes } from './logging/errorCodes';
1213

1314
const { CCPAPurpose } = Constants;
1415

@@ -183,31 +184,36 @@ export default function Consent(this: IConsent, mpInstance: IMParticleWebSDKInst
183184
): PrivacyConsentState | null {
184185
if (typeof consented !== 'boolean') {
185186
mpInstance.Logger.error(
186-
'Consented boolean is required when constructing a Consent object.'
187+
'Consented boolean is required when constructing a Consent object.',
188+
ErrorCodes.CONSENT_ERROR
187189
);
188190
return null;
189191
}
190192
if (timestamp && isNaN(timestamp)) {
191193
mpInstance.Logger.error(
192-
'Timestamp must be a valid number when constructing a Consent object.'
194+
'Timestamp must be a valid number when constructing a Consent object.',
195+
ErrorCodes.CONSENT_ERROR
193196
);
194197
return null;
195198
}
196199
if (consentDocument && typeof consentDocument !== 'string') {
197200
mpInstance.Logger.error(
198-
'Document must be a valid string when constructing a Consent object.'
201+
'Document must be a valid string when constructing a Consent object.',
202+
ErrorCodes.CONSENT_ERROR
199203
);
200204
return null;
201205
}
202206
if (location && typeof location !== 'string') {
203207
mpInstance.Logger.error(
204-
'Location must be a valid string when constructing a Consent object.'
208+
'Location must be a valid string when constructing a Consent object.',
209+
ErrorCodes.CONSENT_ERROR
205210
);
206211
return null;
207212
}
208213
if (hardwareId && typeof hardwareId !== 'string') {
209214
mpInstance.Logger.error(
210-
'Hardware ID must be a valid string when constructing a Consent object.'
215+
'Hardware ID must be a valid string when constructing a Consent object.',
216+
ErrorCodes.CONSENT_ERROR
211217
);
212218
return null;
213219
}
@@ -381,13 +387,14 @@ export default function Consent(this: IConsent, mpInstance: IMParticleWebSDKInst
381387
): ConsentState {
382388
const normalizedPurpose = canonicalizeForDeduplication(purpose);
383389
if (!normalizedPurpose) {
384-
mpInstance.Logger.error('Purpose must be a string.');
390+
mpInstance.Logger.error('Purpose must be a string.', ErrorCodes.CONSENT_ERROR);
385391
return this;
386392
}
387393

388394
if (!isObject(gdprConsent)) {
389395
mpInstance.Logger.error(
390-
'Invoked with a bad or empty consent object.'
396+
'Invoked with a bad or empty consent object.',
397+
ErrorCodes.CONSENT_ERROR
391398
);
392399
return this;
393400
}
@@ -466,7 +473,8 @@ export default function Consent(this: IConsent, mpInstance: IMParticleWebSDKInst
466473
) {
467474
if (!isObject(ccpaConsent)) {
468475
mpInstance.Logger.error(
469-
'Invoked with a bad or empty CCPA consent object.'
476+
'Invoked with a bad or empty CCPA consent object.',
477+
ErrorCodes.CONSENT_ERROR
470478
);
471479
return this;
472480
}

src/ecommerce.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Types from './types';
22
import Constants from './constants';
3+
import { ErrorCodes } from './logging/errorCodes';
34

45
var Messages = Constants.Messages;
56

@@ -116,7 +117,8 @@ export default function Ecommerce(mpInstance) {
116117
mpInstance.Logger.error(
117118
'Could not convert product action type ' +
118119
productActionType +
119-
' to event type'
120+
' to event type',
121+
ErrorCodes.ECOMMERCE_ERROR
120122
);
121123
return null;
122124
}
@@ -132,7 +134,8 @@ export default function Ecommerce(mpInstance) {
132134
mpInstance.Logger.error(
133135
'Could not convert promotion action type ' +
134136
promotionActionType +
135-
' to event type'
137+
' to event type',
138+
ErrorCodes.ECOMMERCE_ERROR
136139
);
137140
return null;
138141
}
@@ -262,20 +265,25 @@ export default function Ecommerce(mpInstance) {
262265
attributes = mpInstance._Helpers.sanitizeAttributes(attributes, name);
263266

264267
if (typeof name !== 'string') {
265-
mpInstance.Logger.error('Name is required when creating a product');
268+
mpInstance.Logger.error(
269+
'Name is required when creating a product',
270+
ErrorCodes.ECOMMERCE_ERROR
271+
);
266272
return null;
267273
}
268274

269275
if (!mpInstance._Helpers.Validators.isStringOrNumber(sku)) {
270276
mpInstance.Logger.error(
271-
'SKU is required when creating a product, and must be a string or a number'
277+
'SKU is required when creating a product, and must be a string or a number',
278+
ErrorCodes.ECOMMERCE_ERROR
272279
);
273280
return null;
274281
}
275282

276283
if (!mpInstance._Helpers.Validators.isStringOrNumber(price)) {
277284
mpInstance.Logger.error(
278-
'Price is required when creating a product, and must be a string or a number'
285+
'Price is required when creating a product, and must be a string or a number',
286+
ErrorCodes.ECOMMERCE_ERROR
279287
);
280288
return null;
281289
} else {
@@ -284,7 +292,8 @@ export default function Ecommerce(mpInstance) {
284292

285293
if (position && !mpInstance._Helpers.Validators.isNumber(position)) {
286294
mpInstance.Logger.error(
287-
'Position must be a number, it will be set to null.'
295+
'Position must be a number, it will be set to null.',
296+
ErrorCodes.ECOMMERCE_ERROR
288297
);
289298
position = null;
290299
}
@@ -312,7 +321,10 @@ export default function Ecommerce(mpInstance) {
312321

313322
this.createPromotion = function(id, creative, name, position) {
314323
if (!mpInstance._Helpers.Validators.isStringOrNumber(id)) {
315-
mpInstance.Logger.error(Messages.ErrorMessages.PromotionIdRequired);
324+
mpInstance.Logger.error(
325+
Messages.ErrorMessages.PromotionIdRequired,
326+
ErrorCodes.ECOMMERCE_ERROR
327+
);
316328
return null;
317329
}
318330

@@ -327,14 +339,16 @@ export default function Ecommerce(mpInstance) {
327339
this.createImpression = function(name, product) {
328340
if (typeof name !== 'string') {
329341
mpInstance.Logger.error(
330-
'Name is required when creating an impression.'
342+
'Name is required when creating an impression.',
343+
ErrorCodes.ECOMMERCE_ERROR
331344
);
332345
return null;
333346
}
334347

335348
if (!product) {
336349
mpInstance.Logger.error(
337-
'Product is required when creating an impression.'
350+
'Product is required when creating an impression.',
351+
ErrorCodes.ECOMMERCE_ERROR
338352
);
339353
return null;
340354
}
@@ -355,7 +369,8 @@ export default function Ecommerce(mpInstance) {
355369
) {
356370
if (!mpInstance._Helpers.Validators.isStringOrNumber(id)) {
357371
mpInstance.Logger.error(
358-
Messages.ErrorMessages.TransactionIdRequired
372+
Messages.ErrorMessages.TransactionIdRequired,
373+
ErrorCodes.ECOMMERCE_ERROR
359374
);
360375
return null;
361376
}

src/events.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Types from './types';
22
import Constants from './constants';
3+
import { ErrorCodes } from './logging/errorCodes';
34

45
var Messages = Constants.Messages;
56

@@ -67,9 +68,10 @@ export default function Events(mpInstance) {
6768
}
6869
} catch (e) {
6970
mpInstance.Logger.error(
70-
'Error invoking the callback passed to startTrackingLocation.'
71+
'Error invoking the callback passed to startTrackingLocation.',
72+
ErrorCodes.EVENTS_ERROR
7173
);
72-
mpInstance.Logger.error(e);
74+
mpInstance.Logger.error(e, ErrorCodes.EVENTS_ERROR);
7375
}
7476
}
7577
}
@@ -224,7 +226,10 @@ export default function Events(mpInstance) {
224226
customFlags
225227
) {
226228
if (!transactionAttributes) {
227-
mpInstance.Logger.error(Messages.ErrorMessages.TransactionRequired);
229+
mpInstance.Logger.error(
230+
Messages.ErrorMessages.TransactionRequired,
231+
ErrorCodes.EVENTS_ERROR
232+
);
228233
return;
229234
}
230235

@@ -329,7 +334,8 @@ export default function Events(mpInstance) {
329334
commerceEvent.EventCategory === null
330335
) {
331336
mpInstance.Logger.error(
332-
'Commerce event not sent. The mParticle.ProductActionType you passed was invalid. Re-check your code.'
337+
'Commerce event not sent. The mParticle.ProductActionType you passed was invalid. Re-check your code.',
338+
ErrorCodes.EVENTS_ERROR
333339
);
334340
return;
335341
}
@@ -414,7 +420,10 @@ export default function Events(mpInstance) {
414420
i;
415421

416422
if (!selector) {
417-
mpInstance.Logger.error("Can't bind event, selector is required");
423+
mpInstance.Logger.error(
424+
"Can't bind event, selector is required",
425+
ErrorCodes.EVENTS_ERROR
426+
);
418427
return;
419428
}
420429

src/forwarders.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import APIClient from './apiClient';
77

88
const { Modify, Identify, Login, Logout } = Constants.IdentityMethods;
99

10+
import { ErrorCodes } from './logging/errorCodes';
11+
1012
export default function Forwarders(mpInstance, kitBlocker) {
1113
var self = this;
1214
this.forwarderStatsUploader = new APIClient(
@@ -443,7 +445,7 @@ export default function Forwarders(mpInstance, kitBlocker) {
443445
mpInstance.Logger.verbose(result);
444446
}
445447
} catch (e) {
446-
mpInstance.Logger.error(e);
448+
mpInstance.Logger.error(e, ErrorCodes.FORWARDERS_ERROR);
447449
}
448450
});
449451
};
@@ -592,7 +594,8 @@ export default function Forwarders(mpInstance, kitBlocker) {
592594
} catch (e) {
593595
mpInstance.Logger.error(
594596
'MP Kits not configured propertly. Kits may not be initialized. ' +
595-
e
597+
e,
598+
ErrorCodes.FORWARDERS_ERROR
596599
);
597600
}
598601
};
@@ -702,7 +705,8 @@ export default function Forwarders(mpInstance, kitBlocker) {
702705
} catch (e) {
703706
mpInstance.Logger.error(
704707
'Sideloaded Kits not configured propertly. Kits may not be initialized. ' +
705-
e
708+
e,
709+
ErrorCodes.FORWARDERS_ERROR
706710
);
707711
}
708712
};
@@ -772,7 +776,8 @@ export default function Forwarders(mpInstance, kitBlocker) {
772776
} catch (e) {
773777
mpInstance.Logger.error(
774778
'Cookie Sync configs not configured propertly. Cookie Sync may not be initialized. ' +
775-
e
779+
e,
780+
ErrorCodes.FORWARDERS_ERROR
776781
);
777782
}
778783
};

0 commit comments

Comments
 (0)