Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
46 changes: 18 additions & 28 deletions src/logger/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,19 @@ export const ERROR_9 = 309;
export const ERROR_10 = 310;
export const ERROR_11 = 311;
export const ERROR_12 = 312;
export const ERROR_13 = 313;
export const ERROR_14 = 314;
export const ERROR_15 = 315;
export const ERROR_16 = 316;
export const ERROR_17 = 317;
export const ERROR_18 = 318;
export const ERROR_19 = 319;
export const ERROR_20 = 320;
export const ERROR_EVENT_TYPE_FORMAT = 314;
export const ERROR_NOT_PLAIN_OBJECT = 318;
export const ERROR_SIZE_EXCEEDED = 319;
export const ERROR_NOT_FINITE = 320;
export const ERROR_21 = 321;
export const ERROR_22 = 322;
export const ERROR_23 = 323;
export const ERROR_24 = 324;
export const ERROR_25 = 325;
export const ERROR_26 = 326;
export const ERROR_32 = 332;
export const ERROR_33 = 333;
export const ERROR_34 = 334;
export const ERROR_35 = 335;
export const ERROR_36 = 336;
export const ERROR_37 = 337;
export const ERROR_38 = 338;
export const ERROR_NULL = 322;
export const ERROR_TOO_LONG = 323;
export const ERROR_INVALID_KEY_OBJECT = 326;
export const ERROR_INVALID = 332;
export const ERROR_EMPTY = 333;
export const ERROR_EMPTY_ARRAY = 334;
export const ERROR_INVALID_ENUM = 338;
export const ERROR_39 = 339;
export const ERROR_API_KEY = 340;

// node
export const ERROR_1 = 301;
Expand Down Expand Up @@ -132,14 +122,14 @@ export const WARN_8 = 208;
export const WARN_9 = 209;
export const WARN_10 = 210;
export const WARN_11 = 211;
export const WARN_12 = 212;
export const WARN_13 = 213;
export const WARN_SETTING_NULL = 212;
export const WARN_TRIMMING_PROPERTIES = 213;
export const WARN_14 = 214;
export const WARN_15 = 215;
export const WARN_17 = 217;
export const WARN_18 = 218;
export const WARN_19 = 219;
export const WARN_20 = 220;
export const WARN_CONVERTING = 215;
export const WARN_TRIMMING = 217;
export const WARN_NOT_EXISTENT_SPLIT = 218;
export const WARN_LOWERCASE_TRAFFIC_TYPE = 219;
export const WARN_NOT_EXISTENT_TT = 220;
export const WARN_21 = 221;
export const WARN_22 = 222;
export const WARN_23 = 223;
Expand Down
33 changes: 12 additions & 21 deletions src/logger/messages/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERROR_0, ERROR_2, ERROR_3, ERROR_4, ERROR_5, ERROR_7, ERROR_9, ERROR_10, ERROR_11, ERROR_12, ERROR_13, ERROR_14, ERROR_15, ERROR_16, ERROR_17, ERROR_18, ERROR_19, ERROR_20, ERROR_21, ERROR_22, ERROR_23, ERROR_24, ERROR_25, ERROR_26, ERROR_32, ERROR_33, ERROR_34, ERROR_35, ERROR_36, ERROR_37, ERROR_38, ERROR_39, ERROR_API_KEY } from '../constants';
import { ERROR_0, ERROR_2, ERROR_3, ERROR_4, ERROR_5, ERROR_7, ERROR_9, ERROR_10, ERROR_11, ERROR_12, ERROR_EVENT_TYPE_FORMAT, ERROR_NOT_PLAIN_OBJECT, ERROR_SIZE_EXCEEDED, ERROR_NOT_FINITE, ERROR_21, ERROR_NULL, ERROR_TOO_LONG, ERROR_INVALID_KEY_OBJECT, ERROR_INVALID, ERROR_EMPTY, ERROR_EMPTY_ARRAY, ERROR_INVALID_ENUM, ERROR_39 } from '../constants';

export const codesError: [number, string][] = [
[ERROR_0, 'splitio-engine:combiner => Invalid Split provided, no valid conditions found'],
Expand All @@ -11,27 +11,18 @@ export const codesError: [number, string][] = [
[ERROR_10, 'splitio-sync:push-manager => Failed to authenticate for streaming. Error: "%s".'],
[ERROR_11, 'splitio-client:impressions-tracker => Could not store impressions bulk with %s impression%s. Error: %s'],
[ERROR_12, 'splitio-client:impressions-tracker => Impression listener logImpression method threw: %s.'],
[ERROR_13, '%s: attributes must be a plain object.'],
[ERROR_14, '%s: you passed "%s", event_type must adhere to the regular expression /^[a-zA-Z0-9][-_.:a-zA-Z0-9]{0,79}$/g. This means an event_type must be alphanumeric, cannot be more than 80 characters long, and can only include a dash, underscore, period, or colon as separators of alphanumeric characters.'],
[ERROR_15, '%s: you passed a null or undefined event_type, event_type must be a non-empty string.'],
[ERROR_16, '%s: you passed an invalid event_type, event_type must be a non-empty string.'],
[ERROR_17, '%s: you passed an empty event_type, event_type must be a non-empty string.'],
[ERROR_18, '%s: properties must be a plain object.'],
[ERROR_19, '%s: The maximum size allowed for the properties is 32768 bytes, which was exceeded. Event not queued.'],
[ERROR_20, '%s: value must be a finite number.'],
[ERROR_21, 'Client has already been destroyed - no calls possible.'],
[ERROR_22, '%s: you passed a null or undefined %s, %s must be a non-empty string.'],
[ERROR_23, '%s: %s too long, %s must be 250 characters or less.'],
[ERROR_24, '%s: you passed an invalid %s type, %s must be a non-empty string.'],
[ERROR_25, '%s: you passed an empty string, %s must be a non-empty string.'],
[ERROR_26, '%s: Key must be an object with bucketingKey and matchingKey with valid string properties.'],
[ERROR_32, '%s: you passed an invalid %s, %s must be a non-empty string.'],
[ERROR_33, '%s: you passed an empty %s, %s must be a non-empty string.'],
[ERROR_34, '%s: %s must be a non-empty array.'],
[ERROR_35, '%s: you passed a null or undefined traffic_type_name, traffic_type_name must be a non-empty string.'],
[ERROR_36, '%s: you passed an invalid traffic_type_name, traffic_type_name must be a non-empty string.'],
[ERROR_37, '%s: you passed an empty traffic_type_name, traffic_type_name must be a non-empty string.'],
[ERROR_38, 'splitio-settings => You passed an invalid impressionsMode, impressionsMode should be one of the following values: "%s" or "%s". Defaulting to "%s" mode.'],
[ERROR_39, 'Response status is not OK. Status: %s. URL: %s. Message: %s'],
[ERROR_API_KEY, 'Factory instantiation: %s, api_key must be a non-empty string']
// input validation
[ERROR_EVENT_TYPE_FORMAT, '%s: you passed "%s", event_type must adhere to the regular expression /^[a-zA-Z0-9][-_.:a-zA-Z0-9]{0,79}$/g. This means an event_type must be alphanumeric, cannot be more than 80 characters long, and can only include a dash, underscore, period, or colon as separators of alphanumeric characters.'],
[ERROR_NOT_PLAIN_OBJECT, '%s: %s must be a plain object.'],
[ERROR_SIZE_EXCEEDED, '%s: the maximum size allowed for the properties is 32768 bytes, which was exceeded. Event not queued.'],
[ERROR_NOT_FINITE, '%s: value must be a finite number.'],
[ERROR_NULL, '%s: you passed a null or undefined %s. It must be a non-empty string.'],
[ERROR_TOO_LONG, '%s: %s too long. It must have 250 characters or less.'],
[ERROR_INVALID_KEY_OBJECT, '%s: Key must be an object with bucketingKey and matchingKey with valid string properties.'],
[ERROR_INVALID, '%s: you passed an invalid %s. It must be a non-empty string.'],
[ERROR_EMPTY, '%s: you passed an empty %s. It must be a non-empty string.'],
[ERROR_EMPTY_ARRAY, '%s: %s must be a non-empty array.'],
];
18 changes: 10 additions & 8 deletions src/logger/messages/warn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WARN_0, WARN_1, WARN_2, WARN_4, WARN_5, WARN_6, WARN_7, WARN_8, WARN_9, WARN_10, WARN_11, WARN_12, WARN_13, WARN_14, WARN_15, WARN_17, WARN_18, WARN_19, WARN_20, WARN_21, WARN_22, WARN_23, WARN_24, WARN_25, WARN_API_KEY } from '../constants';
import { WARN_0, WARN_1, WARN_2, WARN_4, WARN_5, WARN_6, WARN_7, WARN_8, WARN_9, WARN_10, WARN_11, WARN_SETTING_NULL, WARN_TRIMMING_PROPERTIES, WARN_14, WARN_CONVERTING, WARN_TRIMMING, WARN_NOT_EXISTENT_SPLIT, WARN_LOWERCASE_TRAFFIC_TYPE, WARN_NOT_EXISTENT_TT, WARN_21, WARN_22, WARN_23, WARN_24, WARN_25, WARN_API_KEY } from '../constants';

export const codesWarn: [number, string][] = [
[WARN_0, 'splitio-engine:value => Value %s %sdoesn\'t match with expected type.'],
Expand All @@ -12,14 +12,16 @@ export const codesWarn: [number, string][] = [
[WARN_9, 'splitio-sync:submitters => Droping %s %s after retry. Reason %s.'],
[WARN_10, 'splitio-sync:submitters => Failed to push %s %s, keeping data to retry on next iteration. Reason %s.'],
[WARN_11, 'splitio-client:event-tracker => Failed to queue %s'],
[WARN_12, '%s: Property %s is of invalid type. Setting value to null.'],
[WARN_13, '%s: Event has more than 300 properties. Some of them will be trimmed when processed.'],
[WARN_14, '%s: the SDK is not ready, results may be incorrect. Make sure to wait for SDK readiness before using this method.'],
[WARN_15, '%s: %s "%s" is not of type string, converting.'],
[WARN_17, '%s: %s "%s" has extra whitespace, trimming.'],
[WARN_18, '%s: you passed "%s" that does not exist in this environment, please double check what Splits exist in the web console.'],
[WARN_19, '%s: traffic_type_name should be all lowercase - converting string to lowercase.'],
[WARN_20, '%s: Traffic Type %s does not have any corresponding Splits in this environment, make sure you\'re tracking your events to a valid traffic type defined in the Split console.'],
// input validation
[WARN_SETTING_NULL, '%s: Property "%s" is of invalid type. Setting value to null.'],
[WARN_TRIMMING_PROPERTIES, '%s: Event has more than 300 properties. Some of them will be trimmed when processed.'],
[WARN_CONVERTING, '%s: %s "%s" is not of type string, converting.'],
[WARN_TRIMMING, '%s: %s "%s" has extra whitespace, trimming.'],
[WARN_NOT_EXISTENT_SPLIT, '%s: split "%s" does not exist in this environment, please double check what splits exist in the web console.'],
[WARN_LOWERCASE_TRAFFIC_TYPE, '%s: traffic_type_name should be all lowercase - converting string to lowercase.'],
[WARN_NOT_EXISTENT_TT, '%s: traffic type "%s" does not have any corresponding split in this environment, make sure you\'re tracking your events to a valid traffic type defined in the web console.'],
// initialization / settings validation
[WARN_21, 'splitio-settings => %s integration %s at settings %s invalid. %s'],
[WARN_22, 'Factory instantiation: split filters have been configured but will have no effect if mode is not "%s", since synchronization is being deferred to an external tool.'],
[WARN_23, 'Factory instantiation: split filter at position %s is invalid. It must be an object with a valid filter type ("byName" or "byPrefix") and a list of "values".'],
Expand Down
4 changes: 2 additions & 2 deletions src/utils/inputValidation/__tests__/attributes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERROR_13 } from '../../../logger/constants';
import { ERROR_NOT_PLAIN_OBJECT } from '../../../logger/constants';
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';

import { validateAttributes } from '../attributes';
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('INPUT VALIDATION for Attributes', () => {
const invalidAttribute = invalidAttributes[i];

expect(validateAttributes(loggerMock, invalidAttribute, 'test_method')).toBe(false); // Invalid attribute objects should return false.
expect(loggerMock.error).lastCalledWith(ERROR_13, ['test_method']); // The error should be logged for the invalid attributes map.
expect(loggerMock.error).lastCalledWith(ERROR_NOT_PLAIN_OBJECT, ['test_method', 'attributes']); // The error should be logged for the invalid attributes map.

loggerMock.error.mockClear();
}
Expand Down
60 changes: 27 additions & 33 deletions src/utils/inputValidation/__tests__/event.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import { ERROR_INVALID, ERROR_EMPTY, ERROR_NULL, ERROR_EVENT_TYPE_FORMAT } from '../../../logger/constants';
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';

import { validateEvent } from '../event';

const errorMsgs = {
NULL_EVENT: () => 'you passed a null or undefined event_type, event_type must be a non-empty string.',
WRONG_TYPE_EVENT: () => 'you passed an invalid event_type, event_type must be a non-empty string.',
EMPTY_EVENT: () => 'you passed an empty event_type, event_type must be a non-empty string.',
WRONG_FORMAT_EVENT: (invalidEvent: any) => `you passed "${invalidEvent}", event_type must adhere to the regular expression /^[a-zA-Z0-9][-_.:a-zA-Z0-9]{0,79}$/g. This means an event_type must be alphanumeric, cannot be more than 80 characters long, and can only include a dash, underscore, period, or colon as separators of alphanumeric characters.`
};

const invalidEvents = [
{ event: [], msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: () => { }, msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: false, msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: true, msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: {}, msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: Object.create({}), msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: 'something+withInvalidchars', msg: errorMsgs.WRONG_FORMAT_EVENT },
{ event: 'with spaces', msg: errorMsgs.WRONG_FORMAT_EVENT },
{ event: ' asd', msg: errorMsgs.WRONG_FORMAT_EVENT },
{ event: 'asd ', msg: errorMsgs.WRONG_FORMAT_EVENT },
{ event: '?', msg: errorMsgs.WRONG_FORMAT_EVENT },
{ event: '', msg: errorMsgs.EMPTY_EVENT },
{ event: NaN, msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: -Infinity, msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: Infinity, msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: new Promise(res => res), msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: Symbol('asd'), msg: errorMsgs.WRONG_TYPE_EVENT },
{ event: null, msg: errorMsgs.NULL_EVENT },
{ event: undefined, msg: errorMsgs.NULL_EVENT }
{ event: [], msg: ERROR_INVALID },
{ event: () => { }, msg: ERROR_INVALID },
{ event: false, msg: ERROR_INVALID },
{ event: true, msg: ERROR_INVALID },
{ event: {}, msg: ERROR_INVALID },
{ event: Object.create({}), msg: ERROR_INVALID },
{ event: 'something+withInvalidchars', msg: ERROR_EVENT_TYPE_FORMAT },
{ event: 'with spaces', msg: ERROR_EVENT_TYPE_FORMAT },
{ event: ' asd', msg: ERROR_EVENT_TYPE_FORMAT },
{ event: 'asd ', msg: ERROR_EVENT_TYPE_FORMAT },
{ event: '?', msg: ERROR_EVENT_TYPE_FORMAT },
{ event: '', msg: ERROR_EMPTY },
{ event: NaN, msg: ERROR_INVALID },
{ event: -Infinity, msg: ERROR_INVALID },
{ event: Infinity, msg: ERROR_INVALID },
{ event: new Promise(res => res), msg: ERROR_INVALID },
{ event: Symbol('asd'), msg: ERROR_INVALID },
{ event: null, msg: ERROR_NULL },
{ event: undefined, msg: ERROR_NULL }
];

describe('INPUT VALIDATION for Event types', () => {
Expand All @@ -38,26 +32,26 @@ describe('INPUT VALIDATION for Event types', () => {
test('Should return the provided event type if it is a valid string without logging any errors', () => {

expect(validateEvent(loggerMock, 'valid:Too', 'some_method_eventType')).toBe('valid:Too'); // It should return the provided string if it is valid.
expect(loggerMock.error.mock.calls.length).toBe(0); // Should not log any errors.
expect(loggerMock.error).not.toBeCalled(); // Should not log any errors.
expect(validateEvent(loggerMock, 'I.am.valid-string_ValUe', 'some_method_eventType')).toBe('I.am.valid-string_ValUe'); // It should return the provided string if it is valid.
expect(loggerMock.error.mock.calls.length).toBe(0); // Should not log any errors.
expect(loggerMock.error).not.toBeCalled(); // Should not log any errors.
expect(validateEvent(loggerMock, 'a', 'some_method_eventType')).toBe('a'); // It should return the provided string if it is valid.
expect(loggerMock.error.mock.calls.length).toBe(0); // Should not log any errors.
expect(loggerMock.error).not.toBeCalled(); // Should not log any errors.

expect(loggerMock.warn.mock.calls.length).toBe(0); // It should have not logged any warnings.
expect(loggerMock.warn).not.toBeCalled(); // It should have not logged any warnings.
});

test('Should return false and log error if event type is not a valid string', () => {
for (let i = 0; i < invalidEvents.length; i++) {
const invalidValue = invalidEvents[i]['event'];
const expectedLog = invalidEvents[i]['msg'](invalidValue);
const expectedLog = invalidEvents[i]['msg'];

expect(validateEvent(loggerMock, invalidValue, 'test_method')).toBe(false); // Invalid event types should always return false.
expect(loggerMock.error.mock.calls[0][0]).toEqual(`test_method: ${expectedLog}`); // Should log the error for the invalid event type.
expect(loggerMock.error).toBeCalledWith(expectedLog, expectedLog === ERROR_EVENT_TYPE_FORMAT ? ['test_method', invalidValue] : ['test_method']); // Should log the error for the invalid event type.

loggerMock.error.mockClear();
}

expect(loggerMock.warn.mock.calls.length).toBe(0); // It should have not logged any warnings.
expect(loggerMock.warn).not.toBeCalled(); // It should have not logged any warnings.
});
});
Loading