Skip to content

Commit

Permalink
catch and handle credential parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
popestr committed Oct 1, 2024
1 parent ead1219 commit 31db95c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/chat-headless-react/THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The following npm packages may be included in this product:
- @types/[email protected]
- @types/[email protected]
- @types/[email protected]
- @types/[email protected].9
- @types/[email protected].10
- @types/[email protected]

These packages each contain the following license and notice below:
Expand Down
23 changes: 19 additions & 4 deletions packages/chat-headless/src/ChatHeadlessImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,14 @@ export class ChatHeadlessImpl implements ChatHeadless {
this.credentialsSessionStorageKey
);
if (credentials) {
return JSON.parse(credentials);
try {
return JSON.parse(credentials);
} catch (e) {
this.removeSessionAgentCredentials();
throw new Error(
`Error occurred while parsing credentials from session storage: ${e}`
);
}
}
}
}
Expand Down Expand Up @@ -265,9 +272,17 @@ export class ChatHeadlessImpl implements ChatHeadless {
}

this.credentialsSessionStorageKey = `${BASE_HANDOFF_CREDENTIALS_SESSION_STORAGE_KEY}__${hostname}__${this.config.botId}`;

if (this.sessionAgentCredentials) {
this.handoff();

try {
const credentials = this.sessionAgentCredentials;
if (credentials) {
this.handoff();
}
} catch (e) {
console.error(
"Error occurred while initializing agent session using stored credentials:",
e
);
}
}

Expand Down
38 changes: 38 additions & 0 deletions packages/chat-headless/tests/chatheadless.clients.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,44 @@ it("does not reinitialize session if saveToLocalStorage is false", async () => {
expect(botClient.getNextMessage).toHaveBeenCalledTimes(2);
});

it("defaults to bot client if session storage contains invalid data", async () => {
const botClient = createMockHttpClient([
{ message: createMessage("message 1"), notes: {}, integrationDetails: {} }, //trigger handoff
{ message: createMessage("message 2"), notes: {} },
]);
const callbacks: Record<string, any[]> = {};
let agentClient = createMockEventClient(callbacks);
const newConfig = { ...config, saveToLocalStorage: true };
let headless = provideChatHeadless(newConfig, {
bot: botClient,
agent: agentClient,
});

// start with bot client, immediately trigger handoff
await headless.getNextMessage();
expect(botClient.getNextMessage).toHaveBeenCalledTimes(1);
expect(agentClient.init).toHaveBeenCalledTimes(1);

// save invalid data to session storage
sessionStorage.setItem(
"yext_chat_handoff_credentials__localhost__botId",
"invalid"
);

agentClient = createMockEventClient(callbacks);
headless = provideChatHeadless(newConfig, {
bot: botClient,
agent: agentClient,
});

// agent does not reinitialize, nothing saved to session
expect(agentClient.reinitializeSession).toHaveBeenCalledTimes(0);

// bot client is the active client
await headless.getNextMessage();
expect(botClient.getNextMessage).toHaveBeenCalledTimes(2);
});

function createMessage(text: string): Message {
return {
text,
Expand Down

0 comments on commit 31db95c

Please sign in to comment.