Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/amazonq/src/lsp/chat/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import { ChatResult } from '@aws/language-server-runtimes/protocol'
import { ResponseError } from '@aws/language-server-runtimes/protocol'
/**
* Perform a sanity check that the error we got from the LSP can be safely cast to the expected type.
* @param error
* @returns
*/
export function isValidResponseError(error: unknown): error is ResponseError<ChatResult> & { data: ChatResult } {
return (
typeof error === 'object' &&
error !== null &&
'code' in error &&
typeof error.code === 'number' &&
'message' in error &&
typeof error.message === 'string' &&
'data' in error &&
error.data !== undefined
)
}
23 changes: 9 additions & 14 deletions packages/amazonq/src/lsp/chat/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { AuthUtil } from 'aws-core-vscode/codewhisperer'
import { amazonQDiffScheme, AmazonQPromptSettings, messages, openUrl } from 'aws-core-vscode/shared'
import { DefaultAmazonQAppInitContext, messageDispatcher, EditorContentController } from 'aws-core-vscode/amazonq'
import { telemetry, TelemetryBase } from 'aws-core-vscode/telemetry'
import { isValidResponseError } from './error'

export function registerLanguageServerEventListener(languageClient: LanguageClient, provider: AmazonQChatViewProvider) {
languageClient.info(
Expand Down Expand Up @@ -255,22 +256,16 @@ export function registerMessageListeners(
chatDisposable
)
} catch (e) {
languageClient.info(`Error occurred during chat request: ${e}`)
// Use the last partial result if available, append error message
let body = ''
if (!cancellationToken.token.isCancellationRequested) {
body = lastPartialResult?.body
? `${lastPartialResult.body}\n\n ❌ Error: Request failed to complete`
: '❌ An error occurred while processing your request'
}

const errorResult: ChatResult = {
...lastPartialResult,
body,
const errorMsg = `Error occurred during chat request: ${e}`
Copy link
Contributor

@jpinkney-aws jpinkney-aws Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah maybe it does make more sense to just put this on client side, it's really not much extra code and it looks like it makes the error handling contract a lot easier to work with

languageClient.info(errorMsg)
languageClient.info(
`Last result from langauge server: ${JSON.stringify(lastPartialResult, undefined, 2)}`
)
if (!isValidResponseError(e)) {
throw e
}

await handleCompleteResult<ChatResult>(
errorResult,
e.data,
encryptionKey,
provider,
chatParams.tabId,
Expand Down
15 changes: 15 additions & 0 deletions packages/amazonq/test/unit/amazonq/lsp/chat/error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

import { isValidResponseError } from '../../../../../src/lsp/chat/error'
import { ResponseError } from '@aws/language-server-runtimes/protocol'
import * as assert from 'assert'

describe('isValidResponseError', async function () {
it('requires the data field', function () {
assert.ok(isValidResponseError(new ResponseError(0, 'this one has data', {})))
assert.ok(!isValidResponseError(new ResponseError(0, 'this one does not have data')))
})
})
Loading