Skip to content

Commit

Permalink
Integrate error types across all clients
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Jul 29, 2024
1 parent eba1eaa commit 825f103
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 87 deletions.
21 changes: 11 additions & 10 deletions src/internal/event-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,17 @@ export class EventBridgeClient extends AWSClient {
const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
headers: signedRequest.headers,
})
this._handle_error(EventBridgeOperation.PutEvents, res)
this.handleError(res, EventBridgeOperation.PutEvents)
}

_handle_error(
operation: EventBridgeOperation,
response: RefinedResponse<ResponseType | undefined>
) {
const errorCode = response.error_code
if (errorCode === 0) {
return

protected handleError(response: RefinedResponse<ResponseType | undefined>, operation?: string): boolean {
const errored = super.handleError(response, operation);
if (!errored) {
return false
}

const errorCode = response.error_code
const error = response.json() as JSONObject
if (errorCode >= 1400 && errorCode <= 1499) {
// In the event of certain errors, the message is not set.
Expand All @@ -95,16 +94,18 @@ export class EventBridgeClient extends AWSClient {
}

// Otherwise throw a standard service error
throw new EventBridgeServiceError(errorMessage, error.__type as string, operation)
throw new EventBridgeServiceError(errorMessage, error.__type as string, operation as EventBridgeOperation)
}

if (errorCode === 1500) {
throw new EventBridgeServiceError(
'An error occured on the server side',
'InternalServiceError',
operation
operation as EventBridgeOperation
)
}

return true
}
}

Expand Down
25 changes: 11 additions & 14 deletions src/internal/kinesis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,23 +281,18 @@ export class KinesisClient extends AWSClient {
headers: signedRequest.headers,
})

this._handle_error(action, res)
this.handleError(res, action)
return res
}

/**
* If the response is an error, throw an error
*
* @param {string} operation - The name of the operation that was called.
* @param response - RefinedResponse<ResponseType | undefined>
* @returns The response is being returned.
*/
_handle_error(operation: string, response: RefinedResponse<ResponseType | undefined>) {
const errorCode = response.error_code
if (errorCode === 0) {
return

protected handleError(response: RefinedResponse<ResponseType | undefined>, operation?: string): boolean {
const errored = super.handleError(response, operation);
if (!errored) {
return false
}

const errorCode = response.error_code
const error = response.json() as JSONObject
if (errorCode >= 1400 && errorCode <= 1499) {
// In the event of certain errors, the message is not set.
Expand All @@ -311,16 +306,18 @@ export class KinesisClient extends AWSClient {
}

// Otherwise throw a standard service error
throw new KinesisServiceError(errorMessage, error.__type as string, operation)
throw new KinesisServiceError(errorMessage, error.__type as string, operation || 'Unknown')
}

if (errorCode === 1500) {
throw new KinesisServiceError(
'An error occured on the server side',
'InternalServiceError',
operation
operation || 'Unknown'
)
}

return true
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/internal/kms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class KMSClient extends AWSClient {
const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
headers: signedRequest.headers,
})
this._handle_error(KMSOperation.ListKeys, res)
this.handleError(res, KMSOperation.ListKeys)

const json: JSONArray = res.json('Keys') as JSONArray
return json.map((k) => KMSKey.fromJSON(k as JSONObject))
Expand Down Expand Up @@ -114,17 +114,18 @@ export class KMSClient extends AWSClient {
const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
headers: signedRequest.headers,
})
this._handle_error(KMSOperation.GenerateDataKey, res)
this.handleError(res, KMSOperation.GenerateDataKey)

return KMSDataKey.fromJSON(res.json() as JSONObject)
}

_handle_error(operation: KMSOperation, response: RefinedResponse<ResponseType | undefined>) {
const errorCode = response.error_code
if (errorCode === 0) {
return
protected handleError(response: RefinedResponse<ResponseType | undefined>, operation?: string): boolean {
const errored = super.handleError(response, operation);
if (!errored) {
return false
}

const errorCode = response.error_code
const error = response.json() as JSONObject
if (errorCode >= 1400 && errorCode <= 1499) {
// In the event of certain errors, the message is not set.
Expand All @@ -138,16 +139,18 @@ export class KMSClient extends AWSClient {
}

// Otherwise throw a standard service error
throw new KMSServiceError(errorMessage, error.__type as string, operation)
throw new KMSServiceError(errorMessage, error.__type as string, operation as KMSOperation)
}

if (errorCode === 1500) {
throw new KMSServiceError(
'An error occured on the server side',
'InternalServiceError',
operation
operation as KMSOperation
)
}

return true
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/internal/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class LambdaClient extends AWSClient {
const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
headers: signedRequest.headers,
})
this._handle_error(res)
this.handleError(res)

const logResult = res.headers['X-Amz-Log-Result']
const response = {
Expand All @@ -99,12 +99,11 @@ export class LambdaClient extends AWSClient {
}
}

private _handle_error(response: RefinedResponse<ResponseType | undefined>) {
const errorCode: number = response.error_code
const errorMessage: string = response.error

if (errorMessage == '' && errorCode === 0) {
return
protected handleError(response: RefinedResponse<ResponseType | undefined>, operation?: string): boolean {
const errored = super.handleError(response, operation);
if (!errored) {
return false;
}

const awsError = AWSError.parse(response)
Expand All @@ -115,6 +114,8 @@ export class LambdaClient extends AWSClient {
default:
throw awsError
}

return true
}
}

Expand Down
43 changes: 20 additions & 23 deletions src/internal/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class S3Client extends AWSClient {
const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
})
this._handle_error('ListBuckets', res)
this.handleError(res, 'ListBuckets')

const buckets: Array<S3Bucket> = []

Expand Down Expand Up @@ -122,7 +122,7 @@ export class S3Client extends AWSClient {
const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
})
this._handle_error('ListObjectsV2', res)
this.handleError(res, 'ListObjectsV2')

const objects: Array<S3Object> = []

Expand Down Expand Up @@ -185,7 +185,7 @@ export class S3Client extends AWSClient {
const res = await http.asyncRequest(method, signedRequest.url, null, {
headers: signedRequest.headers,
})
this._handle_error('GetObject', res)
this.handleError(res, 'GetObject')

return new S3Object(
objectKey,
Expand Down Expand Up @@ -243,7 +243,7 @@ export class S3Client extends AWSClient {
const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body, {
headers: signedRequest.headers,
})
this._handle_error('PutObject', res)
this.handleError(res, 'PutObject')
}

/**
Expand Down Expand Up @@ -272,7 +272,7 @@ export class S3Client extends AWSClient {
const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
})
this._handle_error('DeleteObject', res)
this.handleError(res, 'DeleteObject')
}

/**
Expand Down Expand Up @@ -312,7 +312,7 @@ export class S3Client extends AWSClient {
headers: signedRequest.headers,
})

this._handle_error('CopyObject', res)
this.handleError(res, 'CopyObject')
}

/**
Expand Down Expand Up @@ -345,7 +345,7 @@ export class S3Client extends AWSClient {
const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
})
this._handle_error('CreateMultipartUpload', res)
this.handleError(res, 'CreateMultipartUpload')

return new S3MultipartUpload(
objectKey,
Expand Down Expand Up @@ -395,7 +395,7 @@ export class S3Client extends AWSClient {
const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
})
this._handle_error('UploadPart', res)
this.handleError(res, 'UploadPart')

return new S3Part(partNumber, res.headers['Etag'])
}
Expand Down Expand Up @@ -445,8 +445,7 @@ export class S3Client extends AWSClient {
const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
})

this._handle_error('CompleteMultipartUpload', res)
this.handleError(res, 'CompleteMultipartUpload')
}

/**
Expand Down Expand Up @@ -480,35 +479,33 @@ export class S3Client extends AWSClient {
const res = await http.asyncRequest(method, signedRequest.url, signedRequest.body || null, {
headers: signedRequest.headers,
})
this._handle_error('AbortMultipartUpload', res)
this.handleError(res, 'AbortMultipartUpload')
}

_handle_error(operation: S3Operation, response: RefinedResponse<ResponseType | undefined>) {
const status: number = response.status
const errorCode: number = response.error_code
const errorMessage: string = response.error

// We consider codes 200-299 as success
if (status >= 200 && status < 300 && errorMessage == '' && errorCode === 0) {
return
}
handleError(response: RefinedResponse<ResponseType | undefined>, operation?: string): boolean {
// As we are overriding the AWSClient method: call the parent class handleError method
const errored = super.handleError(response);
if (!errored) {
return false;
}

// A 301 response is returned when the bucket is not found.
// Generally meaning that either the bucket name is wrong or the
// region is wrong.
//
// See: https://github.com/grafana/k6/issues/2474
// See: https://github.com/golang/go/issues/49281
if (status == 301 || (errorMessage && errorMessage.startsWith('301'))) {
throw new S3ServiceError('Resource not found', 'ResourceNotFound', operation)
const errorMessage: string = response.error
if (response.status == 301 || (errorMessage && errorMessage.startsWith('301'))) {
throw new S3ServiceError('Resource not found', 'ResourceNotFound', operation as S3Operation)
}

const awsError = AWSError.parseXML(response.body as string)
switch (awsError.code) {
case 'AuthorizationHeaderMalformed':
throw new InvalidSignatureError(awsError.message, awsError.code)
default:
throw new S3ServiceError(awsError.message, awsError.code || 'unknown', operation)
throw new S3ServiceError(awsError.message, awsError.code || 'unknown', operation as S3Operation)
}
}
}
Expand Down
29 changes: 15 additions & 14 deletions src/internal/secrets-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class SecretsManagerClient extends AWSClient {
const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
headers: signedRequest.headers,
})
this._handle_error(SecretsManagerOperation.ListSecrets, res)
this.handleError(res, SecretsManagerOperation.ListSecrets)
const json: JSONArray = res.json('SecretList') as JSONArray

return json.map((s) => Secret.fromJSON(s as JSONObject))
Expand Down Expand Up @@ -103,7 +103,7 @@ export class SecretsManagerClient extends AWSClient {
headers: signedRequest.headers,
})

this._handle_error(SecretsManagerOperation.GetSecretValue, res)
this.handleError(res, SecretsManagerOperation.GetSecretValue)

return Secret.fromJSON(res.json() as JSONObject)
}
Expand Down Expand Up @@ -162,7 +162,7 @@ export class SecretsManagerClient extends AWSClient {
const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
headers: signedRequest.headers,
})
this._handle_error(SecretsManagerOperation.CreateSecret, res)
this.handleError(res, SecretsManagerOperation.CreateSecret)

return Secret.fromJSON(res.json() as JSONObject)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ export class SecretsManagerClient extends AWSClient {
const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
headers: signedRequest.headers,
})
this._handle_error(SecretsManagerOperation.PutSecretValue, res)
this.handleError(res, SecretsManagerOperation.PutSecretValue)

return Secret.fromJSON(res.json() as JSONObject)
}
Expand Down Expand Up @@ -251,18 +251,17 @@ export class SecretsManagerClient extends AWSClient {
const res = await http.asyncRequest(this.method, signedRequest.url, signedRequest.body, {
headers: signedRequest.headers,
})
this._handle_error(SecretsManagerOperation.DeleteSecret, res)
this.handleError(res, SecretsManagerOperation.DeleteSecret)
}

_handle_error(
operation: SecretsManagerOperation,
response: RefinedResponse<ResponseType | undefined>
) {
const errorCode = response.error_code
if (errorCode === 0) {
return

protected handleError(response: RefinedResponse<ResponseType | undefined>, operation?: string): boolean {
const errored = super.handleError(response, operation)
if (!errored) {
return false
}

const errorCode = response.error_code
const error = response.json() as JSONObject
if (errorCode >= 1400 && errorCode <= 1499) {
// In the event of certain errors, the message is not set.
Expand All @@ -276,16 +275,18 @@ export class SecretsManagerClient extends AWSClient {
}

// Otherwise throw a standard service error
throw new SecretsManagerServiceError(errorMessage, error.__type as string, operation)
throw new SecretsManagerServiceError(errorMessage, error.__type as string, operation as SecretsManagerOperation)
}

if (errorCode === 1500) {
throw new SecretsManagerServiceError(
'An error occured on the server side',
'InternalServiceError',
operation
operation as SecretsManagerOperation
)
}

return true
}
}

Expand Down
Loading

0 comments on commit 825f103

Please sign in to comment.