Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/contact-center/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"deploy:npm": "yarn npm publish"
},
"dependencies": {
"@webex/contact-center": "3.10.0-next.20",
"@webex/contact-center": "3.10.0-next.33",
"mobx": "6.13.5",
"typescript": "5.6.3"
},
Expand Down
55 changes: 30 additions & 25 deletions packages/contact-center/store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,33 +133,38 @@ class Store implements IStore {
reject(new Error('Webex SDK failed to initialize'));
}, 6000);

//@ts-expect-error To be fixed in SDK - https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6762
const webex = Webex.init({
config: options.webexConfig,
credentials: {
access_token: options.access_token,
},
});
try {
//@ts-expect-error To be fixed in SDK - https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6762
const webex = Webex.init({
config: options.webexConfig,
credentials: {
access_token: options.access_token,
},
});

webex.once('ready', () => {
setupEventListeners(webex.cc);
clearTimeout(timer);
this.registerCC(webex)
.then(() => {
this.logger.log('CC-Widgets: Store init(): store initialization complete', {
module: 'cc-store#store.ts',
method: 'init',
webex.once('ready', () => {
setupEventListeners(webex.cc);
Copy link
Contributor

@bhabalan bhabalan Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If setupEventListeners throws, we aren't doing a clearTimeout. Should we wrap the body of the entire arrow function inside a try catch and clear the timeout if anything happens and reject?

clearTimeout(timer);
this.registerCC(webex)
.then(() => {
this.logger.log('CC-Widgets: Store init(): store initialization complete', {
module: 'cc-store#store.ts',
method: 'init',
});
resolve();
})
.catch((error) => {
this.logger.error(`CC-Widgets: Store init(): registration failed - ${error}`, {
module: 'cc-store#store.ts',
method: 'init',
});
reject(error);
});
resolve();
})
.catch((error) => {
this.logger.error(`CC-Widgets: Store init(): registration failed - ${error}`, {
module: 'cc-store#store.ts',
method: 'init',
});
reject(error);
});
});
});
} catch (error) {
clearTimeout(timer);
reject(error);
}
});
}
}
Expand Down
10 changes: 9 additions & 1 deletion packages/contact-center/store/src/storeEventsWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,15 @@ class StoreWrapper implements IStoreWrapper {
};

init(options: InitParams): Promise<void> {
return this.store.init(options, this.setupIncomingTaskHandler);
return this.store.init(options, this.setupIncomingTaskHandler).catch((error) => {
const err = error instanceof Error ? error : new Error(String(error));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const err = error instanceof Error ? error : new Error(String(error));
const err = error instanceof Error
? error
: new Error(`Store initialization failed: ${String(error)}`);


if (this.onErrorCallback) {
this.onErrorCallback('Store', err);
}

throw err;
});
}

registerCC = (webex?: WithWebex['webex']) => {
Expand Down
45 changes: 43 additions & 2 deletions packages/contact-center/store/tests/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,34 @@ describe('Store', () => {
expect(storeInstance.registerCC).toHaveBeenCalledWith(mockWebex);
});

it('should reject the promise if registerCC fails in init method', async () => {
it('should log an error and reject the promise if registerCC fails in init method', async () => {
const initParams = {
webexConfig: {anyConfig: true},
access_token: 'fake_token',
};

jest.spyOn(storeInstance, 'registerCC').mockRejectedValue(new Error('registerCC failed'));
const error = new Error('registerCC failed');
jest.spyOn(storeInstance, 'registerCC').mockRejectedValue(error);

// Provide a logger so the init() error handler can log without failing
// @ts-expect-error partial logger mock for test
storeInstance.logger = {
error: jest.fn(),
log: jest.fn(),
warn: jest.fn(),
info: jest.fn(),
trace: jest.fn(),
};

await expect(storeInstance.init(initParams, jest.fn())).rejects.toThrow('registerCC failed');

expect(storeInstance.logger.error).toHaveBeenCalledWith(
'CC-Widgets: Store init(): registration failed - Error: registerCC failed',
{
module: 'cc-store#store.ts',
method: 'init',
}
);
});

it('should reject the promise if Webex SDK fails to initialize', async () => {
Expand All @@ -206,5 +225,27 @@ describe('Store', () => {

await expect(initPromise).rejects.toThrow('Webex SDK failed to initialize');
});

it('should clear timeout and reject if Webex.init throws synchronously', async () => {
const initParams = {
webexConfig: {anyConfig: true},
access_token: 'fake_token',
};

const syncError = new Error('sync init error');
// @ts-expect-error overriding mock implementation for this test
const initSpy = jest.spyOn(Webex, 'init').mockImplementation(() => {
throw syncError;
});

await expect(storeInstance.init(initParams, jest.fn())).rejects.toThrow('sync init error');

expect(initSpy).toHaveBeenCalledWith({
config: initParams.webexConfig,
credentials: {
access_token: initParams.access_token,
},
});
});
});
});
46 changes: 46 additions & 0 deletions packages/contact-center/store/tests/storeEventsWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,52 @@ describe('storeEventsWrapper', () => {
expect(mockTask.on).toHaveBeenCalledWith(TASK_EVENTS.AGENT_CONSULT_CREATED, expect.any(Function));
});

it('should call onErrorCallback and rethrow when store.init rejects with an Error', async () => {
const cc = storeWrapper['store'].cc;
const logger = storeWrapper['store'].logger;
const error = new Error('init failed');
const onErrorCallback = jest.fn();

storeWrapper['store'].init = jest.fn().mockRejectedValue(error);
// Directly set onErrorCallback to focus on init error handling behavior
storeWrapper.onErrorCallback = onErrorCallback;

const options = {
webex: {
cc,
logger,
},
};

await expect(storeWrapper.init(options)).rejects.toThrow('init failed');

expect(onErrorCallback).toHaveBeenCalledWith('Store', error);
});

it('should wrap non-Error rejections and pass wrapped Error to onErrorCallback', async () => {
const cc = storeWrapper['store'].cc;
const logger = storeWrapper['store'].logger;
const rawError = 'init failed as string';
const onErrorCallback = jest.fn();

storeWrapper['store'].init = jest.fn().mockRejectedValue(rawError);
storeWrapper.onErrorCallback = onErrorCallback;

const options = {
webex: {
cc,
logger,
},
};

await expect(storeWrapper.init(options)).rejects.toThrow('init failed as string');

expect(onErrorCallback).toHaveBeenCalledWith('Store', expect.any(Error));
const [, wrappedError] = onErrorCallback.mock.calls[0];
expect(wrappedError).toBeInstanceOf(Error);
expect(wrappedError.message).toBe('init failed as string');
});

it('should handle task assignment and call onTaskAssigned callback', () => {
const mockTaskAssignedCallback = jest.fn();
storeWrapper.setTaskAssigned(mockTaskAssignedCallback);
Expand Down
2 changes: 1 addition & 1 deletion widgets-samples/cc/samples-cc-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"react-dom": "18.3.1",
"ts-loader": "^9.5.1",
"typescript": "^5.6.3",
"webex": "3.9.0-next.30",
"webex": "3.10.0-next.49",
"webpack": "^5.94.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.1.0",
Expand Down
14 changes: 10 additions & 4 deletions widgets-samples/cc/samples-cc-react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,16 @@ function App() {
disabled={accessToken.trim() === ''}
onClick={() => {
setShowLoader(true);
store.init({webexConfig, access_token: accessToken}).then(() => {
setIsSdkReady(true);
setShowLoader(false);
});
store
.init({webexConfig, access_token: accessToken})
.then(() => {
setIsSdkReady(true);
setShowLoader(false);
})
.catch((error) => {
console.error('Failed to initialize widgets:', error);
setShowLoader(false);
});
}}
data-testid="samples:init-widgets-button"
>
Expand Down
2 changes: 1 addition & 1 deletion widgets-samples/cc/samples-cc-react-app/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = {
// TS/TSX → transpile only (skip type-checking)
{
test: /\.[jt]sx?$/,
include: [path.resolve(__dirname, 'src'), ...PKG_SRC],
include: [path.resolve(__dirname, 'src'), ...PKG_SRC, resolveMonorepoRoot('node_modules/xxh3-ts')],
loader: 'ts-loader',
options: {
transpileOnly: true, // ✅ disables type-checking
Expand Down
Loading