Skip to content

Commit b425a66

Browse files
authored
fix: emit all errors to get total # of errors (#1252)
* fix: emit more error metrics * fix: getRequestID refactoring
1 parent 9540f12 commit b425a66

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import { ChatSessionManagementService } from '../chat/chatSessionManagementServi
7373
import { ChatTelemetryController } from '../chat/telemetry/chatTelemetryController'
7474
import { QuickAction } from '../chat/quickActions'
7575
import { Metric } from '../../shared/telemetry/metric'
76-
import { getErrorMessage, getHttpStatusCode, isAwsError, isNullish, isObject } from '../../shared/utils'
76+
import { getErrorMessage, getHttpStatusCode, getRequestID, isAwsError, isNullish, isObject } from '../../shared/utils'
7777
import { HELP_MESSAGE, loadingMessage } from '../chat/constants'
7878
import { TelemetryService } from '../../shared/telemetry/telemetryService'
7979
import {
@@ -120,7 +120,7 @@ import {
120120
responseTimeoutPartialMsg,
121121
} from './constants'
122122
import { URI } from 'vscode-uri'
123-
import { AgenticChatError, customerFacingErrorCodes } from './errors'
123+
import { AgenticChatError, customerFacingErrorCodes, unactionableErrorCodes } from './errors'
124124

125125
type ChatHandlers = Omit<
126126
LspHandlers<Chat>,
@@ -1554,22 +1554,22 @@ export class AgenticChatController implements ChatHandlers {
15541554
tabId: string,
15551555
metric: Metric<CombinedConversationEvent>
15561556
): ChatResult | ResponseError<ChatResult> {
1557-
let errorMessage: string | undefined
1558-
let requestID: string | undefined
1559-
if (isAwsError(err) || (isObject(err) && typeof getHttpStatusCode(err) === 'number')) {
1560-
if (err instanceof CodeWhispererStreamingServiceException) {
1561-
errorMessage = err.message
1562-
requestID = err.$metadata.requestId
1563-
} else if (err?.cause?.message) {
1564-
errorMessage = err?.cause?.message
1565-
requestID = err.cause?.$metadata.requestId
1566-
} else if (err instanceof Error || err?.message) {
1567-
errorMessage = err.message
1568-
}
1557+
const errorMessage = getErrorMessage(err)
1558+
const requestID = getRequestID(err) ?? ''
1559+
metric.setDimension('cwsprChatResponseCode', getHttpStatusCode(err) ?? 0)
1560+
metric.setDimension('languageServerVersion', this.#features.runtime.serverInfo.version)
15691561

1570-
metric.setDimension('cwsprChatResponseCode', getHttpStatusCode(err) ?? 0)
1571-
metric.setDimension('languageServerVersion', this.#features.runtime.serverInfo.version)
1572-
this.#telemetryController.emitMessageResponseError(tabId, metric.metric, requestID, errorMessage)
1562+
// use custom error message for unactionable errors (user-dependent errors like PromptCharacterLimit)
1563+
if (err.code && err.code in unactionableErrorCodes) {
1564+
const customErrMessage = unactionableErrorCodes[err.code as keyof typeof unactionableErrorCodes]
1565+
this.#telemetryController.emitMessageResponseError(tabId, metric.metric, requestID, customErrMessage)
1566+
} else {
1567+
this.#telemetryController.emitMessageResponseError(
1568+
tabId,
1569+
metric.metric,
1570+
requestID,
1571+
errorMessage ?? genericErrorMsg
1572+
)
15731573
}
15741574

15751575
let authFollowType: ReturnType<typeof getAuthFollowUpType> = undefined

server/aws-lsp-codewhisperer/src/language-server/agenticChat/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export const customerFacingErrorCodes: AgenticChatErrorCode[] = [
1515
'InputTooLong',
1616
'PromptCharacterLimit',
1717
]
18+
19+
export const unactionableErrorCodes: Partial<Record<AgenticChatErrorCode, string>> = {
20+
PromptCharacterLimit: 'User prompt contains too many characters',
21+
}
22+
1823
export class AgenticChatError extends Error {
1924
constructor(
2025
message: string,

server/aws-lsp-codewhisperer/src/shared/utils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,25 @@ export function parseJson(jsonString: string) {
214214
}
215215

216216
export function getErrorMessage(error: any): string {
217-
if (error instanceof Error) {
217+
if (error?.cause?.message) {
218+
return error?.cause?.message
219+
} else if (error instanceof Error) {
218220
return error.message
219221
}
220222
return String(error)
221223
}
222224

225+
export function getRequestID(error: any): string | undefined {
226+
if (hasCause(error) && error.cause.$metadata?.requestId) {
227+
return error.cause.$metadata.requestId
228+
}
229+
if (error instanceof CodeWhispererStreamingServiceException) {
230+
return error.$metadata.requestId
231+
}
232+
233+
return undefined
234+
}
235+
223236
export function getBearerTokenFromProvider(credentialsProvider: CredentialsProvider) {
224237
if (!credentialsProvider.hasCredentials('bearer')) {
225238
throw new Error(MISSING_BEARER_TOKEN_ERROR)

0 commit comments

Comments
 (0)