Skip to content
Merged
Changes from 1 commit
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
41 changes: 27 additions & 14 deletions packages/amazonq/src/lsp/chat/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,22 +255,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 Expand Up @@ -559,3 +553,22 @@ async function resolveChatResponse(
params: result,
})
}

/**
* Perform a sanity check that the error we got from the LSP can be safely cast to the expected type.
* @param error
* @returns
*/
function isValidResponseError(error: unknown): error is ResponseError<ChatResult> & { data: ChatResult } {
return (
error instanceof ResponseError ||
(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)
)
}
Loading