Skip to content

Commit 5d9cbf3

Browse files
authored
Merge pull request aws#6654 from KevinDing1/master
fix(amazonq): fix uploading file method error handling for /doc
2 parents 4421290 + f556ec8 commit 5d9cbf3

File tree

7 files changed

+82
-40
lines changed

7 files changed

+82
-40
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Amazon Q /doc: Fix uploading file method throwing incorrect workspace too large error message"
4+
}

packages/amazonq/test/unit/amazonqFeatureDev/util/files.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import {
88
prepareRepoData,
99
PrepareRepoDataOptions,
1010
TelemetryHelper,
11-
ContentLengthError,
1211
maxRepoSizeBytes,
1312
} from 'aws-core-vscode/amazonqFeatureDev'
1413
import { assertTelemetry, getWorkspaceFolder, TestFolder } from 'aws-core-vscode/test'
1514
import { fs, AmazonqCreateUpload, ZipStream } from 'aws-core-vscode/shared'
1615
import { MetricName, Span } from 'aws-core-vscode/telemetry'
1716
import sinon from 'sinon'
1817
import { CodeWhispererSettings } from 'aws-core-vscode/codewhisperer'
19-
import { CurrentWsFolders } from 'aws-core-vscode/amazonq'
18+
import { ContentLengthError, CurrentWsFolders } from 'aws-core-vscode/amazonq'
2019
import path from 'path'
2120

2221
const testDevfilePrepareRepo = async (devfileEnabled: boolean) => {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
*/
6+
7+
/**
8+
* Shared error type for content length validation.
9+
* When thrown from common components, individual agents can catch and transform this error
10+
* to provide their own customized error messages.
11+
*/
12+
import { ToolkitError } from '../shared/errors'
13+
14+
export class ContentLengthError extends ToolkitError {
15+
constructor(message: string) {
16+
super(message, { code: 'ContentLengthError' })
17+
}
18+
}

packages/core/src/amazonq/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export { ExtensionMessage } from '../amazonq/webview/ui/commands'
4343
export { CodeReference } from '../codewhispererChat/view/connector/connector'
4444
export { extractAuthFollowUp } from './util/authUtils'
4545
export { Messenger } from './commons/connector/baseMessenger'
46+
export { ContentLengthError } from './errors'
4647
import { FeatureContext } from '../shared/featureConfig'
4748

4849
/**

packages/core/src/amazonq/util/files.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getWorkspaceFoldersByPrefixes,
1313
} from '../../shared/utilities/workspaceUtils'
1414

15-
import { ContentLengthError, PrepareRepoFailedError } from '../../amazonqFeatureDev/errors'
15+
import { PrepareRepoFailedError } from '../../amazonqFeatureDev/errors'
1616
import { getLogger } from '../../shared/logger/logger'
1717
import { maxFileSizeBytes } from '../../amazonqFeatureDev/limits'
1818
import { CurrentWsFolders, DeletedFileInfo, NewFileInfo, NewFileZipContents } from '../../amazonqDoc/types'
@@ -28,6 +28,7 @@ import { ZipStream } from '../../shared/utilities/zipStream'
2828
import { isPresent } from '../../shared/utilities/collectionUtils'
2929
import { AuthUtil } from '../../codewhisperer/util/authUtil'
3030
import { TelemetryHelper } from '../util/telemetryHelper'
31+
import { ContentLengthError } from '../errors'
3132

3233
export const SvgFileExtension = '.svg'
3334

@@ -184,9 +185,9 @@ export async function prepareRepoData(
184185
zipFileChecksum: zipResult.hash,
185186
}
186187
} catch (error) {
187-
getLogger().debug(`featureDev: Failed to prepare repo: ${error}`)
188+
getLogger().debug(`Failed to prepare repo: ${error}`)
188189
if (error instanceof ToolkitError && error.code === 'ContentLengthError') {
189-
throw new ContentLengthError()
190+
throw new ContentLengthError(error.message)
190191
}
191192
throw new PrepareRepoFailedError()
192193
}

packages/core/src/amazonqDoc/session/session.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import fs from '../../shared/fs/fs'
3030
import globals from '../../shared/extensionGlobals'
3131
import { extensionVersion } from '../../shared/vscode/env'
3232
import { getLogger } from '../../shared/logger/logger'
33+
import { ContentLengthError } from '../errors'
34+
import { ContentLengthError as CommonAmazonQContentLengthError } from '../../amazonq/errors'
3335

3436
export class Session {
3537
private _state?: SessionState | Omit<SessionState, 'uploadId'>
@@ -126,28 +128,36 @@ export class Session {
126128
return this.nextInteraction(msg, mode, folderPath)
127129
}
128130
private async nextInteraction(msg: string, mode: Mode, folderPath?: string) {
129-
const resp = await this.state.interact({
130-
task: this.task,
131-
msg,
132-
fs: this.config.fs,
133-
mode: mode,
134-
folderPath: folderPath,
135-
messenger: this.messenger,
136-
telemetry: this.telemetry,
137-
tokenSource: this.state.tokenSource,
138-
uploadHistory: this.state.uploadHistory,
139-
})
131+
try {
132+
const resp = await this.state.interact({
133+
task: this.task,
134+
msg,
135+
fs: this.config.fs,
136+
mode: mode,
137+
folderPath: folderPath,
138+
messenger: this.messenger,
139+
telemetry: this.telemetry,
140+
tokenSource: this.state.tokenSource,
141+
uploadHistory: this.state.uploadHistory,
142+
})
143+
144+
if (resp.nextState) {
145+
if (!this.state?.tokenSource?.token.isCancellationRequested) {
146+
this.state?.tokenSource?.cancel()
147+
}
140148

141-
if (resp.nextState) {
142-
if (!this.state?.tokenSource?.token.isCancellationRequested) {
143-
this.state?.tokenSource?.cancel()
149+
// Move to the next state
150+
this._state = resp.nextState
144151
}
145152

146-
// Move to the next state
147-
this._state = resp.nextState
153+
return resp.interaction
154+
} catch (e) {
155+
if (e instanceof CommonAmazonQContentLengthError) {
156+
getLogger().debug(`Content length validation failed: ${e.message}`)
157+
throw new ContentLengthError()
158+
}
159+
throw e
148160
}
149-
150-
return resp.interaction
151161
}
152162

153163
public async updateFilesPaths(

packages/core/src/amazonqFeatureDev/session/session.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
type SessionStateConfig,
1515
UpdateFilesPathsParams,
1616
} from '../../amazonq/commons/types'
17-
import { ConversationIdNotFoundError } from '../errors'
17+
import { ContentLengthError, ConversationIdNotFoundError } from '../errors'
1818
import { featureDevChat, referenceLogText, featureDevScheme } from '../constants'
1919
import fs from '../../shared/fs/fs'
2020
import { FeatureDevClient } from '../client/featureDev'
@@ -33,6 +33,7 @@ import { UpdateAnswerMessage } from '../../amazonq/commons/connector/connectorMe
3333
import { FollowUpTypes } from '../../amazonq/commons/types'
3434
import { SessionConfig } from '../../amazonq/commons/session/sessionConfigFactory'
3535
import { Messenger } from '../../amazonq/commons/connector/baseMessenger'
36+
import { ContentLengthError as CommonAmazonQContentLengthError } from '../../amazonq/errors'
3637
export class Session {
3738
private _state?: SessionState | Omit<SessionState, 'uploadId'>
3839
private task: string = ''
@@ -137,25 +138,33 @@ export class Session {
137138
}
138139

139140
private async nextInteraction(msg: string) {
140-
const resp = await this.state.interact({
141-
task: this.task,
142-
msg,
143-
fs: this.config.fs,
144-
messenger: this.messenger,
145-
telemetry: this.telemetry,
146-
tokenSource: this.state.tokenSource,
147-
uploadHistory: this.state.uploadHistory,
148-
})
141+
try {
142+
const resp = await this.state.interact({
143+
task: this.task,
144+
msg,
145+
fs: this.config.fs,
146+
messenger: this.messenger,
147+
telemetry: this.telemetry,
148+
tokenSource: this.state.tokenSource,
149+
uploadHistory: this.state.uploadHistory,
150+
})
149151

150-
if (resp.nextState) {
151-
if (!this.state?.tokenSource?.token.isCancellationRequested) {
152-
this.state?.tokenSource?.cancel()
152+
if (resp.nextState) {
153+
if (!this.state?.tokenSource?.token.isCancellationRequested) {
154+
this.state?.tokenSource?.cancel()
155+
}
156+
// Move to the next state
157+
this._state = resp.nextState
153158
}
154-
// Move to the next state
155-
this._state = resp.nextState
156-
}
157159

158-
return resp.interaction
160+
return resp.interaction
161+
} catch (e) {
162+
if (e instanceof CommonAmazonQContentLengthError) {
163+
getLogger().debug(`Content length validation failed: ${e.message}`)
164+
throw new ContentLengthError()
165+
}
166+
throw e
167+
}
159168
}
160169

161170
public async updateFilesPaths(params: UpdateFilesPathsParams) {

0 commit comments

Comments
 (0)