Skip to content

Commit d859821

Browse files
committed
chore: error handling
1 parent 60c98f7 commit d859821

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/agentPreview.ts

+41-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { randomUUID } from 'node:crypto';
9+
import { SfError } from '@salesforce/core';
910
import { Connection } from '@salesforce/core';
1011
import { MaybeMock } from './maybe-mock';
1112

@@ -31,8 +32,20 @@ export type AgentPreviewMessageLinks = {
3132
end: Href | null;
3233
};
3334

35+
export type MessageType =
36+
| 'Inform'
37+
| 'TextChunk'
38+
| 'ProgressIndicator'
39+
| 'Inquire'
40+
| 'Confirm'
41+
| 'Failure'
42+
| 'Escalate'
43+
| 'SessionEnded'
44+
| 'EndOfTurn'
45+
| 'Error';
46+
3447
export type AgentPreviewMessage = {
35-
type: string;
48+
type: MessageType;
3649
id: string;
3750
feedbackId: string;
3851
planId: string;
@@ -98,7 +111,12 @@ export class AgentPreview {
98111
bypassUser: true,
99112
};
100113

101-
return this.maybeMock.request<AgentPreviewStartResponse>('POST', url, body);
114+
try {
115+
return await this.maybeMock.request<AgentPreviewStartResponse>('POST', url, body);
116+
} catch (err) {
117+
const error = err as Error;
118+
throw new SfError(error.message, 'AgentPreviewStartError', undefined, error);
119+
}
102120
}
103121

104122
public async send(sessionId: string, message: string): Promise<AgentPreviewSendResponse> {
@@ -115,23 +133,38 @@ export class AgentPreview {
115133
variables: [],
116134
};
117135

118-
return this.maybeMock.request<AgentPreviewSendResponse>('POST', url, body);
136+
try {
137+
return await this.maybeMock.request<AgentPreviewSendResponse>('POST', url, body);
138+
} catch (err) {
139+
const error = err as Error;
140+
throw new SfError(error.message, 'AgentPreviewSendError', undefined, error);
141+
}
119142
}
120143

121144
public async end(sessionId: string, reason: EndReason): Promise<AgentPreviewEndResponse> {
122145
const url = `${this.apiBase}/sessions/${sessionId}`;
123146

124-
// https://developer.salesforce.com/docs/einstein/genai/guide/agent-api-examples.html#end-session
125-
return this.maybeMock.request<AgentPreviewEndResponse>('DELETE', url, undefined, {
126-
'x-session-end-reason': reason,
127-
});
147+
try {
148+
// https://developer.salesforce.com/docs/einstein/genai/guide/agent-api-examples.html#end-session
149+
return await this.maybeMock.request<AgentPreviewEndResponse>('DELETE', url, undefined, {
150+
'x-session-end-reason': reason,
151+
});
152+
} catch (err) {
153+
const error = err as Error;
154+
throw new SfError(error.message, 'AgentPreviewEndError', undefined, error);
155+
}
128156
}
129157

130158
// Get the status of the Agent API (UP | DOWN)
131159
public async status(): Promise<ApiStatus> {
132160
const base = 'https://test.api.salesforce.com';
133161
const url = `${base}/einstein/ai-agent/v1/status`;
134162

135-
return this.maybeMock.request<ApiStatus>('GET', url);
163+
try {
164+
return await this.maybeMock.request<ApiStatus>('GET', url);
165+
} catch (err) {
166+
const error = err as Error;
167+
throw new SfError(error.message, 'AgentPreviewStatusError', undefined, error);
168+
}
136169
}
137170
}

0 commit comments

Comments
 (0)