Skip to content

Commit 139bb7a

Browse files
feat: add helpful error messages for agent not found cases
Wrap BotDefinition and BotVersion queries in try-catch blocks to provide user-friendly error messages when an agent or version doesn't exist, instead of showing raw SOQL error.
1 parent 62658c9 commit 139bb7a

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

src/commands/org/open/agent.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { Flags } from '@salesforce/sf-plugins-core';
18-
import { Connection, Messages } from '@salesforce/core';
18+
import { Connection, Messages, SfError } from '@salesforce/core';
1919
import { OrgOpenCommandBase } from '../../../shared/orgOpenCommandBase.js';
2020
import { type OrgOpenOutput } from '../../../shared/orgTypes.js';
2121

@@ -90,12 +90,25 @@ export class OrgOpenAgent extends OrgOpenCommandBase<OrgOpenOutput> {
9090
// Build the URL part to the Agent Builder given a Bot API name.
9191
const buildRetUrl = async (conn: Connection, botName: string, version?: string): Promise<string> => {
9292
const query = `SELECT id FROM BotDefinition WHERE DeveloperName='${botName}'`;
93-
const botId = (await conn.singleRecordQuery<{ Id: string }>(query)).Id;
93+
let botId: string;
94+
try {
95+
botId = (await conn.singleRecordQuery<{ Id: string }>(query)).Id;
96+
} catch (error) {
97+
throw new SfError(`No agent found with API name '${botName}' in the target org.`, 'AgentNotFound');
98+
}
99+
94100
const queryParams = new URLSearchParams({ copilotId: botId });
95101
if (version) {
96102
const versionQuery = `SELECT Id FROM BotVersion WHERE BotDefinitionId='${botId}' AND VersionNumber=${version}`;
97-
const versionId = (await conn.singleRecordQuery<{ Id: string }>(versionQuery)).Id;
98-
queryParams.set('versionId', versionId);
103+
try {
104+
const versionId = (await conn.singleRecordQuery<{ Id: string }>(versionQuery)).Id;
105+
queryParams.set('versionId', versionId);
106+
} catch (error) {
107+
throw new SfError(
108+
`No version '${version}' found for agent '${botName}' in the target org.`,
109+
'AgentVersionNotFound'
110+
);
111+
}
99112
}
100113
return `AiCopilot/copilotStudio.app#/copilot/builder?${queryParams.toString()}`;
101114
};

test/unit/org/open/agent.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,60 @@ describe('org:open:agent', () => {
337337
});
338338
});
339339

340+
describe('error handling', () => {
341+
it('throws helpful error when agent API name does not exist', async () => {
342+
const nonExistentAgent = 'NonExistent_Agent';
343+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
344+
spies.get('singleRecordQuery').rejects(new Error('No records found'));
345+
346+
try {
347+
await OrgOpenAgent.run([
348+
'--json',
349+
'--target-org',
350+
testOrg.username,
351+
'--url-only',
352+
'--api-name',
353+
nonExistentAgent,
354+
]);
355+
assert.fail('Should have thrown an error');
356+
} catch (error) {
357+
expect(error).to.exist;
358+
expect((error as Error).message).to.include(`No agent found with API name '${nonExistentAgent}'`);
359+
expect((error as Error).name).to.equal('AgentNotFound');
360+
}
361+
});
362+
363+
it('throws helpful error when agent version does not exist', async () => {
364+
const nonExistentVersion = '999';
365+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
366+
const singleRecordQueryStub = spies.get('singleRecordQuery');
367+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
368+
singleRecordQueryStub.onFirstCall().resolves({ Id: mockBotId }); // BotDefinition succeeds
369+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
370+
singleRecordQueryStub.onSecondCall().rejects(new Error('No records found')); // BotVersion fails
371+
372+
try {
373+
await OrgOpenAgent.run([
374+
'--json',
375+
'--target-org',
376+
testOrg.username,
377+
'--url-only',
378+
'--api-name',
379+
mockBotName,
380+
'--version',
381+
nonExistentVersion,
382+
]);
383+
assert.fail('Should have thrown an error');
384+
} catch (error) {
385+
expect(error).to.exist;
386+
expect((error as Error).message).to.include(
387+
`No version '${nonExistentVersion}' found for agent '${mockBotName}'`
388+
);
389+
expect((error as Error).name).to.equal('AgentVersionNotFound');
390+
}
391+
});
392+
});
393+
340394
describe('api-version flag', () => {
341395
it('respects api-version flag with api-name', async () => {
342396
const apiVersion = '59.0';

0 commit comments

Comments
 (0)