Skip to content

Commit 3d4255e

Browse files
[FSSDK-10935] exception log adjustment
1 parent b9c2faf commit 3d4255e

25 files changed

+115
-50
lines changed

lib/error_messages.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ export const INVALID_INPUT_FORMAT = '%s: Provided %s is in an invalid format.';
7777
export const INVALID_DATAFILE_VERSION =
7878
'%s: This version of the JavaScript SDK does not support the given datafile version: %s';
7979
export const INVALID_VARIATION_KEY = '%s: Provided variation key is in an invalid format.';
80+
export const UNABLE_TO_GET_VUID = 'Unable to get VUID - ODP Manager is not instantiated yet.'
81+
export const ERROR_FETCHING_DATAFILE = 'Error fetching datafile: %s'
82+
export const DATAFILE_FETCH_REQUEST_FAILED = 'Datafile fetch request failed with status: %s'
83+
export const ONRUNNING_ERROR = 'onRunning error'

lib/event_processor/batch_event_processor.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import { isSuccessStatusCode } from "../utils/http_request_handler/http_util";
2727
import { EventEmitter } from "../utils/event_emitter/event_emitter";
2828
import { IdGenerator } from "../utils/id_generator";
2929
import { areEventContextsEqual } from "./event_builder/user_event";
30+
import { EVENT_PROCESSOR_STOPPED, FAILED_TO_DISPATCH_EVENTS, FAILED_TO_DISPATCH_EVENTS_WITH_ARG } from "../exception_messages";
31+
import { sprintf } from "../utils/fns";
3032

3133
export type EventWithId = {
3234
id: string;
@@ -160,7 +162,7 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
160162
const dispatcher = closing && this.closingEventDispatcher ? this.closingEventDispatcher : this.eventDispatcher;
161163
return dispatcher.dispatchEvent(request).then((res) => {
162164
if (res.statusCode && !isSuccessStatusCode(res.statusCode)) {
163-
return Promise.reject(new Error(`Failed to dispatch events: ${res.statusCode}`));
165+
return Promise.reject(new Error(sprintf(FAILED_TO_DISPATCH_EVENTS_WITH_ARG, res.statusCode)));
164166
}
165167
return Promise.resolve(res);
166168
});
@@ -195,7 +197,7 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
195197
}).catch((err) => {
196198
// if the dispatch fails, the events will still be
197199
// in the store for future processing
198-
this.logger?.error('Failed to dispatch events', err);
200+
this.logger?.error(FAILED_TO_DISPATCH_EVENTS, err);
199201
}).finally(() => {
200202
this.runningTask.delete(taskId);
201203
ids.forEach((id) => this.dispatchingEventIds.delete(id));
@@ -253,7 +255,7 @@ export class BatchEventProcessor extends BaseService implements EventProcessor {
253255

254256
if (this.isNew()) {
255257
// TOOD: replace message with imported constants
256-
this.startPromise.reject(new Error('Event processor stopped before it could be started'));
258+
this.startPromise.reject(new Error(EVENT_PROCESSOR_STOPPED));
257259
}
258260

259261
this.state = ServiceState.Stopping;

lib/event_processor/event_dispatcher/default_dispatcher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import { ONLY_POST_REQUESTS_ARE_SUPPORTED } from '../../exception_messages';
1617
import { RequestHandler } from '../../utils/http_request_handler/http';
1718
import { EventDispatcher, EventDispatcherResponse, LogEvent } from './event_dispatcher';
1819

@@ -28,7 +29,7 @@ export class DefaultEventDispatcher implements EventDispatcher {
2829
): Promise<EventDispatcherResponse> {
2930
// Non-POST requests not supported
3031
if (eventObj.httpVerb !== 'POST') {
31-
return Promise.reject(new Error('Only POST requests are supported'));
32+
return Promise.reject(new Error(ONLY_POST_REQUESTS_ARE_SUPPORTED));
3233
}
3334

3435
const dataString = JSON.stringify(eventObj.params);

lib/event_processor/event_dispatcher/send_beacon_dispatcher.browser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { SEND_BEACON_FAILED } from '../../exception_messages';
1718
import { EventDispatcher, EventDispatcherResponse } from './event_dispatcher';
1819

1920
export type Event = {
@@ -41,7 +42,7 @@ export const dispatchEvent = function(
4142
if(success) {
4243
return Promise.resolve({});
4344
}
44-
return Promise.reject(new Error('sendBeacon failed'));
45+
return Promise.reject(new Error(SEND_BEACON_FAILED));
4546
}
4647

4748
const eventDispatcher : EventDispatcher = {

lib/event_processor/event_processor_factory.react_native.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ async function mockRequireNetInfo() {
6060
M._load = (uri: string, parent: string) => {
6161
if (uri === '@react-native-community/netinfo') {
6262
if (isNetInfoAvailable) return {};
63-
throw new Error('Module not found: @react-native-community/netinfo');
63+
throw new Error(MODULE_NOT_FOUND_REACT_NATIVE_NETINFO);
6464
}
6565
if (uri === '@react-native-async-storage/async-storage') {
6666
if (isAsyncStorageAvailable) return {};
67-
throw new Error('Module not found: @react-native-async-storage/async-storage');
67+
throw new Error(MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE);
6868
}
6969

7070
return M._load_original(uri, parent);
@@ -80,6 +80,10 @@ import { AsyncCache, AsyncPrefixCache, SyncCache, SyncPrefixCache } from '../uti
8080
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
8181
import { ReactNativeNetInfoEventProcessor } from './batch_event_processor.react_native';
8282
import { BatchEventProcessor } from './batch_event_processor';
83+
import {
84+
MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE,
85+
MODULE_NOT_FOUND_REACT_NATIVE_NETINFO,
86+
} from '../exception_messages';
8387

8488
describe('createForwardingEventProcessor', () => {
8589
const mockGetForwardingEventProcessor = vi.mocked(getForwardingEventProcessor);
@@ -163,7 +167,7 @@ describe('createBatchEventProcessor', () => {
163167
});
164168

165169
expect(() => createBatchEventProcessor({})).toThrowError(
166-
'Module not found: @react-native-async-storage/async-storage'
170+
MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE
167171
);
168172

169173
isAsyncStorageAvailable = true;

lib/event_processor/forwarding_event_processor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { buildLogEvent } from './event_builder/log_event';
2323
import { BaseService, ServiceState } from '../service';
2424
import { EventEmitter } from '../utils/event_emitter/event_emitter';
2525
import { Consumer, Fn } from '../utils/type';
26+
import { SERVICE_STOPPED_BEFORE_IT_WAS_STARTED } from '../exception_messages';
2627
class ForwardingEventProcessor extends BaseService implements EventProcessor {
2728
private dispatcher: EventDispatcher;
2829
private eventEmitter: EventEmitter<{ dispatch: LogEvent }>;
@@ -54,7 +55,7 @@ class ForwardingEventProcessor extends BaseService implements EventProcessor {
5455
}
5556

5657
if (this.isNew()) {
57-
this.startPromise.reject(new Error('Service stopped before it was started'));
58+
this.startPromise.reject(new Error(SERVICE_STOPPED_BEFORE_IT_WAS_STARTED));
5859
}
5960

6061
this.state = ServiceState.Terminated;

lib/exception_messages.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const FAILED_TO_DISPATCH_EVENTS = 'Failed to dispatch events'
2+
export const FAILED_TO_DISPATCH_EVENTS_WITH_ARG = 'Failed to dispatch events: %s';
3+
export const EVENT_PROCESSOR_STOPPED = 'Event processor stopped before it could be started';
4+
export const SERVICE_STOPPED_BEFORE_IT_WAS_STARTED = 'Service stopped before it was started';
5+
export const ONLY_POST_REQUESTS_ARE_SUPPORTED = 'Only POST requests are supported';
6+
export const SEND_BEACON_FAILED = 'sendBeacon failed';
7+
export const CANNOT_START_WITHOUT_ODP_CONFIG = 'cannot start without ODP config';
8+
export const START_CALLED_WHEN_ODP_IS_NOT_INTEGRATED = 'start() called when ODP is not integrated';
9+
export const ODP_ACTION_IS_NOT_VALID = 'ODP action is not valid (cannot be empty).';
10+
export const ONREADY_TIMEOUT_EXPIRED = 'onReady timeout expired after %s ms';
11+
export const INSTANCE_CLOSED = 'Instance closed';
12+
export const DATAFILE_MANAGER_STOPPED = 'Datafile manager stopped before it could be started';
13+
export const DATAFILE_MANAGER_FAILED_TO_START = 'Datafile manager failed to start';
14+
export const FAILED_TO_FETCH_DATAFILE = 'Failed to fetch datafile';
15+
export const FAILED_TO_STOP = 'Failed to stop';
16+
export const YOU_MUST_PROVIDE_DATAFILE_IN_SSR = 'You must provide datafile in SSR';
17+
export const YOU_MUST_PROVIDE_AT_LEAST_ONE_OF_SDKKEY_OR_DATAFILE = 'You must provide at least one of sdkKey or datafile';
18+
export const RETRY_CANCELLED = 'Retry cancelled';
19+
export const REQUEST_ERROR = 'Request error';
20+
export const UNSUPPORTED_PROTOCOL = 'Unsupported protocol: %s';
21+
export const REQUEST_TIMEOUT = 'Request timed out';
22+
export const NO_STATUS_CODE_IN_RESPONSE = 'No status code in response';
23+
export const MODULE_NOT_FOUND_REACT_NATIVE_ASYNC_STORAGE = 'Module not found: @react-native-async-storage/async-storage';
24+
export const MODULE_NOT_FOUND_REACT_NATIVE_NETINFO = 'Module not found: @react-native-community/netinfo';
25+
export const INVALID_CONFIG_OR_SOMETHING = 'Invalid config or something';
26+
export const PROMISE_SHOULD_NOT_HAVE_RESOLVED = 'Promise should not have resolved';

lib/index.browser.tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { getMockProjectConfigManager } from './tests/mock/mock_project_config_ma
3434
import { createProjectConfig } from './project_config/project_config';
3535
import { ODP_EVENT_FAILED_ODP_MANAGER_MISSING } from './error_messages';
3636
import { ODP_DISABLED, ODP_SEND_EVENT_IDENTIFIER_CONVERSION_FAILED } from './log_messages';
37+
import { INVALID_CONFIG_OR_SOMETHING } from './exception_messages';
3738

3839

3940
class MockLocalStorage {
@@ -163,7 +164,7 @@ describe('javascript-sdk (Browser)', function() {
163164
// });
164165

165166
it('should not throw if the provided config is not valid', function() {
166-
configValidator.validate.throws(new Error('Invalid config or something'));
167+
configValidator.validate.throws(new Error(INVALID_CONFIG_OR_SOMETHING));
167168
assert.doesNotThrow(function() {
168169
var optlyInstance = optimizelyFactory.createInstance({
169170
projectConfigManager: getMockProjectConfigManager(),

lib/index.browser.umdtests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Optimizely from './optimizely';
2323
import testData from './tests/test_data';
2424
import packageJSON from '../package.json';
2525
import eventDispatcher from './plugins/event_dispatcher/index.browser';
26+
import { INVALID_CONFIG_OR_SOMETHING } from './exception_messages';
2627

2728
describe('javascript-sdk', function() {
2829
describe('APIs', function() {
@@ -92,7 +93,7 @@ describe('javascript-sdk', function() {
9293
});
9394

9495
it('should not throw if the provided config is not valid', function() {
95-
configValidator.validate.throws(new Error('Invalid config or something'));
96+
configValidator.validate.throws(new Error(INVALID_CONFIG_OR_SOMETHING));
9697
assert.doesNotThrow(function() {
9798
var optlyInstance = window.optimizelySdk.createInstance({
9899
datafile: {},

lib/index.lite.tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as loggerPlugin from './plugins/logger';
2222
import optimizelyFactory from './index.lite';
2323
import configValidator from './utils/config_validator';
2424
import { getMockProjectConfigManager } from './tests/mock/mock_project_config_manager';
25+
import { INVALID_CONFIG_OR_SOMETHING } from './exception_messages';
2526

2627
describe('optimizelyFactory', function() {
2728
describe('APIs', function() {
@@ -52,7 +53,7 @@ describe('optimizelyFactory', function() {
5253
});
5354

5455
it('should not throw if the provided config is not valid and log an error if logger is passed in', function() {
55-
configValidator.validate.throws(new Error('Invalid config or something'));
56+
configValidator.validate.throws(new Error(INVALID_CONFIG_OR_SOMETHING));
5657
var localLogger = loggerPlugin.createLogger({ logLevel: enums.LOG_LEVEL.INFO });
5758
assert.doesNotThrow(function() {
5859
var optlyInstance = optimizelyFactory.createInstance({

0 commit comments

Comments
 (0)