@@ -9,14 +9,14 @@ import { ConfiguredRetryStrategy } from '@aws-sdk/util-retry'
99import { omit } from 'lodash'
1010import { AuthUtil } from '../../codewhisperer/util/authUtil'
1111import { ServiceOptions } from '../../shared/awsClientBuilder'
12- import { ToolkitError } from '../../shared/errors'
1312import globals from '../../shared/extensionGlobals'
1413import { getLogger } from '../../shared/logger'
1514import * as FeatureDevProxyClient from './featuredevproxyclient'
1615import apiConfig = require( './codewhispererruntime-2022-11-11.json' )
1716import { featureName } from '../constants'
18- import { ContentLengthError } from '../errors'
17+ import { ApiError , ContentLengthError , UnknownApiError } from '../errors'
1918import { endpoint , region } from '../../codewhisperer/models/constants'
19+ import { isAwsError , isCodeWhispererStreamingServiceException } from '../../shared/errors'
2020
2121// Create a client for featureDev proxy client based off of aws sdk v2
2222export async function createFeatureDevProxyClient ( ) : Promise < FeatureDevProxyClient > {
@@ -53,6 +53,15 @@ async function createFeatureDevStreamingClient(): Promise<CodeWhispererStreaming
5353 return streamingClient
5454}
5555
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+
5665export class FeatureDevClient {
5766 private async getClient ( ) {
5867 // Should not be stored for the whole session.
@@ -77,12 +86,14 @@ export class FeatureDevClient {
7786 } )
7887 return conversationId
7988 } 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' )
8697 }
8798 }
8899
@@ -108,16 +119,18 @@ export class FeatureDevClient {
108119 requestId : response . $response . requestId ,
109120 } )
110121 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 )
119131 }
120- throw new ToolkitError ( ( e as Error ) . message , { code : 'CreateUploadUrlFailed' } )
132+
133+ throw new UnknownApiError ( e instanceof Error ? e . message : 'Unknown error' , 'CreateUploadUrl' )
121134 }
122135 }
123136
@@ -153,10 +166,21 @@ export class FeatureDevClient {
153166 }
154167 return assistantResponse . join ( ' ' )
155168 } 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' )
160184 }
161185 }
162186}
0 commit comments