Skip to content

Commit 7c9ee78

Browse files
[FSSDK-10935] log adjustment
1 parent d34f250 commit 7c9ee78

17 files changed

+86
-44
lines changed

lib/error_messages.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export const NO_EVENT_PROCESSOR = 'No event processor is provided';
4949
export const NO_VARIATION_FOR_EXPERIMENT_KEY = '%s: No variation key %s defined in datafile for experiment %s.';
5050
export const ODP_CONFIG_NOT_AVAILABLE = '%s: ODP is not integrated to the project.';
5151
export const ODP_EVENT_FAILED = 'ODP event send failed.';
52+
export const ODP_EVENT_MANAGER_IS_NOT_RUNNING = 'ODP event manager is not running.';
53+
export const ODP_EVENTS_SHOULD_HAVE_ATLEAST_ONE_KEY_VALUE = 'ODP events should have at least one key-value pair in identifiers.';
5254
export const ODP_FETCH_QUALIFIED_SEGMENTS_SEGMENTS_MANAGER_MISSING =
5355
'%s: ODP unable to fetch qualified segments (Segments Manager not initialized).';
5456
export const ODP_IDENTIFY_FAILED_EVENT_MANAGER_MISSING =
@@ -93,7 +95,14 @@ export const INVALID_INPUT_FORMAT = '%s: Provided %s is in an invalid format.';
9395
export const INVALID_DATAFILE_VERSION =
9496
'%s: This version of the JavaScript SDK does not support the given datafile version: %s';
9597
export const INVALID_VARIATION_KEY = '%s: Provided variation key is in an invalid format.';
96-
export const UNABLE_TO_GET_VUID = 'Unable to get VUID - ODP Manager is not instantiated yet.'
97-
export const ERROR_FETCHING_DATAFILE = 'Error fetching datafile: %s'
98-
export const DATAFILE_FETCH_REQUEST_FAILED = 'Datafile fetch request failed with status: %s'
99-
export const ONRUNNING_ERROR = 'onRunning error'
98+
export const UNABLE_TO_GET_VUID = 'Unable to get VUID - ODP Manager is not instantiated yet.';
99+
export const ERROR_FETCHING_DATAFILE = 'Error fetching datafile: %s';
100+
export const DATAFILE_FETCH_REQUEST_FAILED = 'Datafile fetch request failed with status: %s';
101+
export const ONRUNNING_ERROR = 'onRunning error';
102+
export const EVENT_DATA_FOUND_TO_BE_INVALID = 'Event data found to be invalid.';
103+
export const EVENT_ACTION_INVALID = 'Event action invalid.';
104+
export const FAILED_TO_SEND_ODP_EVENTS = 'failed to send odp events';
105+
export const UNABLE_TO_OVERWRITE_GLOBAL_WINDOW = 'Unable to overwrite global.window.';
106+
export const UNABLE_TO_GET_VUID_VUID_MANAGER_NOT_AVAILABLE = 'Unable to get VUID - VuidManager is not available'
107+
108+

lib/exception_messages.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,25 @@ export const SEND_BEACON_FAILED = 'sendBeacon failed';
2323
export const CANNOT_START_WITHOUT_ODP_CONFIG = 'cannot start without ODP config';
2424
export const START_CALLED_WHEN_ODP_IS_NOT_INTEGRATED = 'start() called when ODP is not integrated';
2525
export const ODP_ACTION_IS_NOT_VALID = 'ODP action is not valid (cannot be empty).';
26+
export const ODP_MANAGER_STOPPED_BEFORE_RUNNING = 'odp manager stopped before running';
27+
export const ODP_EVENT_MANAGER_STOPPED = "ODP event manager stopped before it could start";
2628
export const ONREADY_TIMEOUT_EXPIRED = 'onReady timeout expired after %s ms';
2729
export const INSTANCE_CLOSED = 'Instance closed';
2830
export const DATAFILE_MANAGER_STOPPED = 'Datafile manager stopped before it could be started';
2931
export const DATAFILE_MANAGER_FAILED_TO_START = 'Datafile manager failed to start';
3032
export const FAILED_TO_FETCH_DATAFILE = 'Failed to fetch datafile';
33+
export const FAILED_TO_START = 'Failed to start';
3134
export const FAILED_TO_STOP = 'Failed to stop';
3235
export const YOU_MUST_PROVIDE_DATAFILE_IN_SSR = 'You must provide datafile in SSR';
3336
export const YOU_MUST_PROVIDE_AT_LEAST_ONE_OF_SDKKEY_OR_DATAFILE = 'You must provide at least one of sdkKey or datafile';
3437
export const RETRY_CANCELLED = 'Retry cancelled';
3538
export const REQUEST_ERROR = 'Request error';
39+
export const REQUEST_FAILED = 'Request failed';
3640
export const UNSUPPORTED_PROTOCOL = 'Unsupported protocol: %s';
3741
export const REQUEST_TIMEOUT = 'Request timed out';
3842
export const NO_STATUS_CODE_IN_RESPONSE = 'No status code in response';
3943
export const MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE = 'Module not found: @react-native-async-storage/async-storage';
4044
export const MODULE_NOT_FOUND_REACT_NATIVE_NETINFO = 'Module not found: @react-native-community/netinfo';
4145
export const INVALID_CONFIG_OR_SOMETHING = 'Invalid config or something';
4246
export const PROMISE_SHOULD_NOT_HAVE_RESOLVED = 'Promise should not have resolved';
47+
export const VUID_IS_NOT_SUPPORTED_IN_NODEJS= 'VUID is not supported in Node.js environment';

lib/index.browser.tests.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import optimizelyFactory from './index.browser';
2424
import configValidator from './utils/config_validator';
2525
import { getMockProjectConfigManager } from './tests/mock/mock_project_config_manager';
2626
import { createProjectConfig } from './project_config/project_config';
27-
import { ODP_EVENT_FAILED_ODP_MANAGER_MISSING } from './error_messages';
28-
import { ODP_DISABLED, ODP_SEND_EVENT_IDENTIFIER_CONVERSION_FAILED } from './log_messages';
27+
import { UNABLE_TO_OVERWRITE_GLOBAL_WINDOW } from './error_messages';
2928
import { INVALID_CONFIG_OR_SOMETHING } from './exception_messages';
3029

3130

@@ -57,7 +56,7 @@ if (!global.window) {
5756
localStorage: new MockLocalStorage(),
5857
};
5958
} catch (e) {
60-
console.error('Unable to overwrite global.window.');
59+
console.error(UNABLE_TO_OVERWRITE_GLOBAL_WINDOW);
6160
}
6261
}
6362

lib/log_messages.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,9 @@ export const UPDATED_OPTIMIZELY_CONFIG = '%s: Updated Optimizely config to revis
123123
export const OUT_OF_BOUNDS =
124124
'%s: Audience condition %s evaluated to UNKNOWN because the number value for user attribute "%s" is not in the range [-2^53, +2^53].';
125125
export const UNABLE_TO_ATTACH_UNLOAD = '%s: unable to bind optimizely.close() to page unload event: "%s"';
126+
export const UNABLE_TO_PARSE_AND_SKIPPED_HEADER = 'Unable to parse & skipped header item';
127+
export const ADDING_AUTHORIZATION_HEADER_WITH_BEARER_TOKEN = 'Adding Authorization header with Bearer Token';
128+
export const MAKING_DATAFILE_REQ_TO_URL_WITH_HEADERS = 'Making datafile request to url %s with headers: %s';
129+
export const RESPONSE_STATUS_CODE = 'Response status code: %s';
130+
export const SAVED_LAST_MODIFIED_HEADER_VALUE_FROM_RESPONSE = 'Saved last modified header value from response: %s';
131+

lib/odp/event_manager/odp_event_api_manager.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
import { describe, it, expect, vi } from 'vitest';
1817

1918
import { DefaultOdpEventApiManager, eventApiRequestGenerator, pixelApiRequestGenerator } from './odp_event_api_manager';
@@ -42,6 +41,7 @@ const PIXEL_URL = 'https://odp.pixel.com';
4241
const odpConfig = new OdpConfig(API_KEY, API_HOST, PIXEL_URL, []);
4342

4443
import { getMockRequestHandler } from '../../tests/mock/mock_request_handler';
44+
import { REQUEST_FAILED } from '../../exception_messages';
4545

4646
describe('DefaultOdpEventApiManager', () => {
4747
it('should generate the event request using the correct odp config and event', async () => {
@@ -101,7 +101,7 @@ describe('DefaultOdpEventApiManager', () => {
101101
it('should return a promise that fails if the requestHandler response promise fails', async () => {
102102
const mockRequestHandler = getMockRequestHandler();
103103
mockRequestHandler.makeRequest.mockReturnValue({
104-
responsePromise: Promise.reject(new Error('Request failed')),
104+
responsePromise: Promise.reject(new Error(REQUEST_FAILED)),
105105
});
106106
const requestGenerator = vi.fn().mockReturnValue({
107107
method: 'PATCH',

lib/odp/event_manager/odp_event_manager.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { OdpEvent } from './odp_event';
2323
import { OdpConfig } from '../odp_config';
2424
import { EventDispatchResponse } from './odp_event_api_manager';
2525
import { advanceTimersByTime } from '../../../tests/testUtils';
26+
import { FAILED_TO_DISPATCH_EVENTS } from '../../exception_messages';
2627

2728
const API_KEY = 'test-api-key';
2829
const API_HOST = 'https://odp.example.com';
@@ -604,7 +605,7 @@ describe('DefaultOdpEventManager', () => {
604605
const repeater = getMockRepeater();
605606

606607
const apiManager = getMockApiManager();
607-
apiManager.sendEvents.mockReturnValue(Promise.reject(new Error('Failed to dispatch events')));
608+
apiManager.sendEvents.mockReturnValue(Promise.reject(new Error(FAILED_TO_DISPATCH_EVENTS)));
608609

609610
const backoffController = {
610611
backoff: vi.fn().mockReturnValue(666),
@@ -706,7 +707,7 @@ describe('DefaultOdpEventManager', () => {
706707
const repeater = getMockRepeater();
707708

708709
const apiManager = getMockApiManager();
709-
apiManager.sendEvents.mockReturnValue(Promise.reject(new Error('Failed to dispatch events')));
710+
apiManager.sendEvents.mockReturnValue(Promise.reject(new Error(FAILED_TO_DISPATCH_EVENTS)));
710711

711712
const backoffController = {
712713
backoff: vi.fn().mockReturnValue(666),

lib/odp/event_manager/odp_event_manager.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ import { Producer } from '../../utils/type';
2323
import { runWithRetry } from '../../utils/executor/backoff_retry_runner';
2424
import { isSuccessStatusCode } from '../../utils/http_request_handler/http_util';
2525
import { ODP_DEFAULT_EVENT_TYPE, ODP_USER_KEY } from '../constant';
26-
import { ODP_NOT_INTEGRATED } from '../../error_messages';
26+
import {
27+
EVENT_ACTION_INVALID,
28+
EVENT_DATA_FOUND_TO_BE_INVALID,
29+
FAILED_TO_SEND_ODP_EVENTS,
30+
ODP_EVENT_MANAGER_IS_NOT_RUNNING,
31+
ODP_EVENTS_SHOULD_HAVE_ATLEAST_ONE_KEY_VALUE,
32+
ODP_NOT_INTEGRATED,
33+
} from '../../error_messages';
34+
import { sprintf } from '../../utils/fns';
35+
import { FAILED_TO_DISPATCH_EVENTS_WITH_ARG, ODP_EVENT_MANAGER_STOPPED } from '../../exception_messages';
2736

2837
export interface OdpEventManager extends Service {
2938
updateConfig(odpIntegrationConfig: OdpIntegrationConfig): void;
@@ -66,8 +75,7 @@ export class DefaultOdpEventManager extends BaseService implements OdpEventManag
6675
private async executeDispatch(odpConfig: OdpConfig, batch: OdpEvent[]): Promise<unknown> {
6776
const res = await this.apiManager.sendEvents(odpConfig, batch);
6877
if (res.statusCode && !isSuccessStatusCode(res.statusCode)) {
69-
// TODO: replace message with imported constants
70-
return Promise.reject(new Error(`Failed to dispatch events: ${res.statusCode}`));
78+
return Promise.reject(new Error(sprintf(FAILED_TO_DISPATCH_EVENTS_WITH_ARG, res.statusCode)));
7179
}
7280
return await Promise.resolve(res);
7381
}
@@ -89,8 +97,7 @@ export class DefaultOdpEventManager extends BaseService implements OdpEventManag
8997
return runWithRetry(
9098
() => this.executeDispatch(odpConfig, batch), this.retryConfig.backoffProvider(), this.retryConfig.maxRetries
9199
).result.catch((err) => {
92-
// TODO: replace with imported constants
93-
this.logger?.error('failed to send odp events', err);
100+
this.logger?.error(FAILED_TO_SEND_ODP_EVENTS, err);
94101
});
95102
}
96103

@@ -139,7 +146,7 @@ export class DefaultOdpEventManager extends BaseService implements OdpEventManag
139146
}
140147

141148
if (this.isNew()) {
142-
this.startPromise.reject(new Error('odp event manager stopped before it could start'));
149+
this.startPromise.reject(new Error(ODP_EVENT_MANAGER_STOPPED));
143150
}
144151

145152
this.flush();
@@ -149,7 +156,7 @@ export class DefaultOdpEventManager extends BaseService implements OdpEventManag
149156

150157
sendEvent(event: OdpEvent): void {
151158
if (!this.isRunning()) {
152-
this.logger?.error('ODP event manager is not running.');
159+
this.logger?.error(ODP_EVENT_MANAGER_IS_NOT_RUNNING);
153160
return;
154161
}
155162

@@ -159,17 +166,17 @@ export class DefaultOdpEventManager extends BaseService implements OdpEventManag
159166
}
160167

161168
if (event.identifiers.size === 0) {
162-
this.logger?.error('ODP events should have at least one key-value pair in identifiers.');
169+
this.logger?.error(ODP_EVENTS_SHOULD_HAVE_ATLEAST_ONE_KEY_VALUE);
163170
return;
164171
}
165172

166173
if (!this.isDataValid(event.data)) {
167-
this.logger?.error('Event data found to be invalid.');
174+
this.logger?.error(EVENT_DATA_FOUND_TO_BE_INVALID);
168175
return;
169176
}
170177

171178
if (!event.action ) {
172-
this.logger?.error('Event action invalid.');
179+
this.logger?.error(EVENT_ACTION_INVALID);
173180
return;
174181
}
175182

lib/odp/odp_manager.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { ODP_USER_KEY } from './constant';
2525
import { OptimizelySegmentOption } from './segment_manager/optimizely_segment_option';
2626
import { OdpEventManager } from './event_manager/odp_event_manager';
2727
import { CLIENT_VERSION, JAVASCRIPT_CLIENT_ENGINE } from '../utils/enums';
28+
import { FAILED_TO_START, FAILED_TO_STOP } from '../exception_messages';
2829

2930
const keyA = 'key-a';
3031
const hostA = 'host-a';
@@ -166,7 +167,7 @@ describe('DefaultOdpManager', () => {
166167
await exhaustMicrotasks();
167168

168169
expect(odpManager.getState()).toEqual(ServiceState.Starting);
169-
eventManagerPromise.reject(new Error('Failed to start'));
170+
eventManagerPromise.reject(new Error(FAILED_TO_START));
170171

171172
await expect(odpManager.onRunning()).rejects.toThrow();
172173
await expect(odpManager.onTerminated()).rejects.toThrow();
@@ -186,7 +187,7 @@ describe('DefaultOdpManager', () => {
186187
odpManager.start();
187188
expect(odpManager.getState()).toEqual(ServiceState.Starting);
188189

189-
eventManagerPromise.reject(new Error('Failed to start'));
190+
eventManagerPromise.reject(new Error(FAILED_TO_START));
190191

191192
await expect(odpManager.onRunning()).rejects.toThrow();
192193
await expect(odpManager.onTerminated()).rejects.toThrow();
@@ -692,7 +693,7 @@ describe('DefaultOdpManager', () => {
692693
await exhaustMicrotasks();
693694
expect(odpManager.getState()).toEqual(ServiceState.Stopping);
694695

695-
eventManagerTerminatedPromise.reject(new Error('Failed to stop'));
696+
eventManagerTerminatedPromise.reject(new Error(FAILED_TO_STOP));
696697
await expect(odpManager.onTerminated()).rejects.toThrow();
697698
});
698699
});

lib/odp/odp_manager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { CLIENT_VERSION, JAVASCRIPT_CLIENT_ENGINE } from '../utils/enums';
2929
import { ODP_DEFAULT_EVENT_TYPE, ODP_EVENT_ACTION, ODP_USER_KEY } from './constant';
3030
import { isVuid } from '../vuid/vuid';
3131
import { Maybe } from '../utils/type';
32+
import { ODP_MANAGER_STOPPED_BEFORE_RUNNING } from '../exception_messages';
3233

3334
export interface OdpManager extends Service {
3435
updateConfig(odpIntegrationConfig: OdpIntegrationConfig): boolean;
@@ -131,7 +132,7 @@ export class DefaultOdpManager extends BaseService implements OdpManager {
131132
}
132133

133134
if (!this.isRunning()) {
134-
this.startPromise.reject(new Error('odp manager stopped before running'));
135+
this.startPromise.reject(new Error(ODP_MANAGER_STOPPED_BEFORE_RUNNING));
135136
}
136137

137138
this.state = ServiceState.Stopping;

lib/odp/segment_manager/odp_segment_api_manager.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ODP_USER_KEY } from '../constant';
2020
import { getMockRequestHandler } from '../../tests/mock/mock_request_handler';
2121
import { getMockLogger } from '../../tests/mock/mock_logger';
2222
import { DefaultOdpSegmentApiManager } from './odp_segment_api_manager';
23+
import { REQUEST_TIMEOUT } from '../../exception_messages';
2324

2425
const API_KEY = 'not-real-api-key';
2526
const GRAPHQL_ENDPOINT = 'https://some.example.com/graphql/endpoint';
@@ -46,7 +47,7 @@ describe('DefaultOdpSegmentApiManager', () => {
4647
const requestHandler = getMockRequestHandler();
4748
requestHandler.makeRequest.mockReturnValue({
4849
abort: () => {},
49-
responsePromise: Promise.reject(new Error('Request timed out')),
50+
responsePromise: Promise.reject(new Error(REQUEST_TIMEOUT)),
5051
});
5152
const logger = getMockLogger();
5253
const manager = new DefaultOdpSegmentApiManager(requestHandler, logger);

0 commit comments

Comments
 (0)