@@ -9,14 +9,14 @@ import { ConfiguredRetryStrategy } from '@aws-sdk/util-retry'
9
9
import { omit } from 'lodash'
10
10
import { AuthUtil } from '../../codewhisperer/util/authUtil'
11
11
import { ServiceOptions } from '../../shared/awsClientBuilder'
12
- import { ToolkitError } from '../../shared/errors'
13
12
import globals from '../../shared/extensionGlobals'
14
13
import { getLogger } from '../../shared/logger'
15
14
import * as FeatureDevProxyClient from './featuredevproxyclient'
16
15
import apiConfig = require( './codewhispererruntime-2022-11-11.json' )
17
16
import { featureName } from '../constants'
18
- import { ContentLengthError } from '../errors'
17
+ import { ApiError , ContentLengthError , UnknownApiError } from '../errors'
19
18
import { endpoint , region } from '../../codewhisperer/models/constants'
19
+ import { isAwsError , isCodeWhispererStreamingServiceException } from '../../shared/errors'
20
20
21
21
// Create a client for featureDev proxy client based off of aws sdk v2
22
22
export async function createFeatureDevProxyClient ( ) : Promise < FeatureDevProxyClient > {
@@ -53,6 +53,15 @@ async function createFeatureDevStreamingClient(): Promise<CodeWhispererStreaming
53
53
return streamingClient
54
54
}
55
55
56
+ const streamResponseErrors : Record < string , number > = {
57
+ ValidationException : 400 ,
58
+ AccessDeniedException : 403 ,
59
+ ResourceNotFoundException : 404 ,
60
+ ConflictException : 409 ,
61
+ ThrottlingException : 429 ,
62
+ InternalServerException : 500 ,
63
+ }
64
+
56
65
export class FeatureDevClient {
57
66
private async getClient ( ) {
58
67
// Should not be stored for the whole session.
@@ -77,12 +86,14 @@ export class FeatureDevClient {
77
86
} )
78
87
return conversationId
79
88
} catch ( e ) {
80
- getLogger ( ) . error (
81
- `${ featureName } : failed to start conversation: ${ ( e as Error ) . message } RequestId: ${
82
- ( e as any ) . requestId
83
- } `
84
- )
85
- throw new ToolkitError ( ( e as Error ) . message , { code : 'CreateConversationFailed' } )
89
+ if ( isAwsError ( e ) ) {
90
+ getLogger ( ) . error (
91
+ `${ featureName } : failed to start conversation: ${ e . message } RequestId: ${ e . requestId } `
92
+ )
93
+ throw new ApiError ( e . message , 'CreateConversation' , e . code , e . statusCode ?? 400 )
94
+ }
95
+
96
+ throw new UnknownApiError ( e instanceof Error ? e . message : 'Unknown error' , 'CreateConversation' )
86
97
}
87
98
}
88
99
@@ -108,16 +119,18 @@ export class FeatureDevClient {
108
119
requestId : response . $response . requestId ,
109
120
} )
110
121
return response
111
- } catch ( e : any ) {
112
- getLogger ( ) . error (
113
- `${ featureName } : failed to generate presigned url: ${ ( e as Error ) . message } RequestId: ${
114
- ( e as any ) . requestId
115
- } `
116
- )
117
- if ( e . code === 'ValidationException' && e . message . includes ( 'Invalid contentLength' ) ) {
118
- throw new ContentLengthError ( )
122
+ } catch ( e ) {
123
+ if ( isAwsError ( e ) ) {
124
+ getLogger ( ) . error (
125
+ `${ featureName } : failed to generate presigned url: ${ e . message } RequestId: ${ e . requestId } `
126
+ )
127
+ if ( e . code === 'ValidationException' && e . message . includes ( 'Invalid contentLength' ) ) {
128
+ throw new ContentLengthError ( )
129
+ }
130
+ throw new ApiError ( e . message , 'CreateUploadUrl' , e . code , e . statusCode ?? 400 )
119
131
}
120
- throw new ToolkitError ( ( e as Error ) . message , { code : 'CreateUploadUrlFailed' } )
132
+
133
+ throw new UnknownApiError ( e instanceof Error ? e . message : 'Unknown error' , 'CreateUploadUrl' )
121
134
}
122
135
}
123
136
@@ -153,10 +166,21 @@ export class FeatureDevClient {
153
166
}
154
167
return assistantResponse . join ( ' ' )
155
168
} catch ( e ) {
156
- getLogger ( ) . error (
157
- `${ featureName } : failed to execute planning: ${ ( e as Error ) . message } RequestId: ${ ( e as any ) . requestId } `
158
- )
159
- throw new ToolkitError ( ( e as Error ) . message , { code : 'GeneratePlanFailed' } )
169
+ if ( isCodeWhispererStreamingServiceException ( e ) ) {
170
+ getLogger ( ) . error (
171
+ `${ featureName } : failed to execute planning: ${ e . message } RequestId: ${
172
+ e . $metadata . requestId ?? 'unknown'
173
+ } `
174
+ )
175
+ throw new ApiError (
176
+ e . message ,
177
+ 'GeneratePlan' ,
178
+ e . name ,
179
+ e . $metadata ?. httpStatusCode ?? streamResponseErrors [ e . name ] ?? 500
180
+ )
181
+ }
182
+
183
+ throw new UnknownApiError ( e instanceof Error ? e . message : 'Unknown error' , 'GeneratePlan' )
160
184
}
161
185
}
162
186
}
0 commit comments