Skip to content

Commit e1f6e79

Browse files
fix(cc-store): fix store register method error handling (#590)
1 parent 2341423 commit e1f6e79

9 files changed

Lines changed: 692 additions & 964 deletions

File tree

packages/contact-center/store/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"deploy:npm": "yarn npm publish"
2424
},
2525
"dependencies": {
26-
"@webex/contact-center": "3.10.0-next.20",
26+
"@webex/contact-center": "3.10.0-next.33",
2727
"mobx": "6.13.5",
2828
"typescript": "5.6.3"
2929
},

packages/contact-center/store/src/store.ts

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,33 +133,49 @@ class Store implements IStore {
133133
reject(new Error('Webex SDK failed to initialize'));
134134
}, 6000);
135135

136-
//@ts-expect-error To be fixed in SDK - https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6762
137-
const webex = Webex.init({
138-
config: options.webexConfig,
139-
credentials: {
140-
access_token: options.access_token,
141-
},
142-
});
136+
try {
137+
//@ts-expect-error To be fixed in SDK - https://jira-eng-sjc12.cisco.com/jira/browse/CAI-6762
138+
const webex = Webex.init({
139+
config: options.webexConfig,
140+
credentials: {
141+
access_token: options.access_token,
142+
},
143+
});
143144

144-
webex.once('ready', () => {
145-
setupEventListeners(webex.cc);
146-
clearTimeout(timer);
147-
this.registerCC(webex)
148-
.then(() => {
149-
this.logger.log('CC-Widgets: Store init(): store initialization complete', {
150-
module: 'cc-store#store.ts',
151-
method: 'init',
152-
});
153-
resolve();
154-
})
155-
.catch((error) => {
156-
this.logger.error(`CC-Widgets: Store init(): registration failed - ${error}`, {
157-
module: 'cc-store#store.ts',
158-
method: 'init',
159-
});
145+
webex.once('ready', () => {
146+
try {
147+
setupEventListeners(webex.cc);
148+
clearTimeout(timer);
149+
this.registerCC(webex)
150+
.then(() => {
151+
this.logger.log('CC-Widgets: Store init(): store initialization complete', {
152+
module: 'cc-store#store.ts',
153+
method: 'init',
154+
});
155+
resolve();
156+
})
157+
.catch((error) => {
158+
this.logger.error(`CC-Widgets: Store init(): registration failed - ${error}`, {
159+
module: 'cc-store#store.ts',
160+
method: 'init',
161+
});
162+
reject(error);
163+
});
164+
} catch (error) {
165+
clearTimeout(timer);
166+
if (this.logger) {
167+
this.logger.error(`CC-Widgets: Store init(): setupEventListeners failed - ${error}`, {
168+
module: 'cc-store#store.ts',
169+
method: 'init',
170+
});
171+
}
160172
reject(error);
161-
});
162-
});
173+
}
174+
});
175+
} catch (error) {
176+
clearTimeout(timer);
177+
reject(error);
178+
}
163179
});
164180
}
165181
}

packages/contact-center/store/src/storeEventsWrapper.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,15 @@ class StoreWrapper implements IStoreWrapper {
384384
};
385385

386386
init(options: InitParams): Promise<void> {
387-
return this.store.init(options, this.setupIncomingTaskHandler);
387+
return this.store.init(options, this.setupIncomingTaskHandler).catch((error) => {
388+
const err = error instanceof Error ? error : new Error(`Store initialization failed: ${String(error)}`);
389+
390+
if (this.onErrorCallback) {
391+
this.onErrorCallback('Store', err);
392+
}
393+
394+
throw err;
395+
});
388396
}
389397

390398
registerCC = (webex?: WithWebex['webex']) => {

packages/contact-center/store/tests/store.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,33 @@ describe('Store', () => {
179179
expect(storeInstance.registerCC).toHaveBeenCalledWith(mockWebex);
180180
});
181181

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

188-
jest.spyOn(storeInstance, 'registerCC').mockRejectedValue(new Error('registerCC failed'));
188+
const error = new Error('registerCC failed');
189+
jest.spyOn(storeInstance, 'registerCC').mockRejectedValue(error);
190+
191+
// Provide a logger so the init() error handler can log without failing
192+
storeInstance.logger = {
193+
error: jest.fn(),
194+
log: jest.fn(),
195+
warn: jest.fn(),
196+
info: jest.fn(),
197+
trace: jest.fn(),
198+
};
189199

190200
await expect(storeInstance.init(initParams, jest.fn())).rejects.toThrow('registerCC failed');
201+
202+
expect(storeInstance.logger.error).toHaveBeenCalledWith(
203+
'CC-Widgets: Store init(): registration failed - Error: registerCC failed',
204+
{
205+
module: 'cc-store#store.ts',
206+
method: 'init',
207+
}
208+
);
191209
});
192210

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

207225
await expect(initPromise).rejects.toThrow('Webex SDK failed to initialize');
208226
});
227+
228+
it('should clear timeout and reject if Webex.init throws synchronously', async () => {
229+
const initParams = {
230+
webexConfig: {anyConfig: true},
231+
access_token: 'fake_token',
232+
};
233+
234+
const syncError = new Error('sync init error');
235+
// @ts-expect-error overriding mock implementation for this test
236+
const initSpy = jest.spyOn(Webex, 'init').mockImplementation(() => {
237+
throw syncError;
238+
});
239+
240+
await expect(storeInstance.init(initParams, jest.fn())).rejects.toThrow('sync init error');
241+
242+
expect(initSpy).toHaveBeenCalledWith({
243+
config: initParams.webexConfig,
244+
credentials: {
245+
access_token: initParams.access_token,
246+
},
247+
});
248+
});
209249
});
210250
});

0 commit comments

Comments
 (0)