Skip to content

Commit

Permalink
fix: replace err.code with err.name
Browse files Browse the repository at this point in the history
  • Loading branch information
rene84 committed Nov 17, 2023
1 parent 6937ffd commit 73c45b2
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 57 deletions.
37 changes: 19 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"license": "MIT",
"dependencies": {
"@aws-sdk/client-cloudformation": "^3.451.0",
"@aws-sdk/client-cloudwatch-events": "^3.451.0",
"@aws-sdk/client-ec2": "^3.451.0",
"@aws-sdk/client-eventbridge": "3.451.0",
"@aws-sdk/client-iam": "^3.451.0",
"@aws-sdk/client-organizations": "^3.451.0",
"@aws-sdk/client-s3": "^3.451.0",
Expand Down
5 changes: 3 additions & 2 deletions src/aws-provider/aws-events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PutEventsCommand, CloudWatchEventsClient } from '@aws-sdk/client-cloudwatch-events';
import { PutEventsCommand } from '@aws-sdk/client-eventbridge';
import { ConsoleUtil } from '../util/console-util';
import { AwsUtil } from '~util/aws-util';

const eventSource = 'oc.org-formation';
const eventDetailType = 'events.org-formation.com';
Expand Down Expand Up @@ -48,7 +49,7 @@ export class AwsEvents {
}

public static async PutEvent(command: PutEventsCommand): Promise<void> {
const events = new CloudWatchEventsClient({region: 'us-east-1'});
const events = AwsUtil.GetEventBridgeService(undefined, 'us-east-1');
await events.send(command);
}
}
22 changes: 11 additions & 11 deletions src/aws-provider/aws-organization-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class AwsOrganizationWriter {
await org.send(enablePolicyTypeCommand);
ConsoleUtil.LogDebug('enabled service control policies');
} catch (err) {
if (err && err.code === 'PolicyTypeAlreadyEnabledException') {
if (err && err.name === 'PolicyTypeAlreadyEnabledException') {
// do nothing
} else {
throw err;
Expand All @@ -66,7 +66,7 @@ export class AwsOrganizationWriter {
ConsoleUtil.LogDebug(`SCP Created ${scpId}`);
return scpId;
} catch (err) {
if (err.code === 'DuplicatePolicyException') {
if (err.name === 'DuplicatePolicyException') {
const existingPolicy: AWSPolicy = this.organization.policies.find(x => x.Name === resource.policyName);
const scpId = existingPolicy!.Id;
await this.updatePolicy(resource, scpId);
Expand All @@ -91,15 +91,15 @@ export class AwsOrganizationWriter {
await this.ensureSCPEnabled();
await org.send(attachPolicyCommand);
} catch (err) {
if (err && err.code === 'PolicyTypeNotEnabledException') {
if (err && err.name === 'PolicyTypeNotEnabledException') {
await this.ensureSCPEnabled();
await org.send(attachPolicyCommand);
} else {
throw err;
}
}
} catch (err) {
if (err && err.code !== 'DuplicatePolicyAttachmentException') {
if (err && err.name !== 'DuplicatePolicyAttachmentException') {
throw err;
}
}
Expand All @@ -115,7 +115,7 @@ export class AwsOrganizationWriter {
try {
await this.organizationsService.send(detachPolicyCommand);
} catch (err) {
if (err && err.code !== 'PolicyNotAttachedException' && err.code !== 'PolicyNotFoundException') {
if (err && err.name !== 'PolicyNotAttachedException' && err.name !== 'PolicyNotFoundException') {
// 'ConcurrentModificationException' ??
throw err;
}
Expand Down Expand Up @@ -143,7 +143,7 @@ export class AwsOrganizationWriter {
try {
await this.organizationsService.send(deletePolicyCommand);
} catch (err) {
if (err && err.code !== 'PolicyNotFoundException' && err.code !== 'PolicyInUseException') {
if (err && err.name !== 'PolicyNotFoundException' && err.name !== 'PolicyInUseException') {
// 'ConcurrentModificationException' ??
throw err;
}
Expand Down Expand Up @@ -335,7 +335,7 @@ export class AwsOrganizationWriter {
try {
await this.updateAccount(resource, accountId);
} catch (err) {
if (err.code === 'AccessDenied' && retryCountAccessDenied < 3) {
if (err.name === 'AccessDenied' && retryCountAccessDenied < 3) {
shouldRetry = true;
retryCountAccessDenied = retryCountAccessDenied + 1;
await sleep(3000);
Expand Down Expand Up @@ -374,7 +374,7 @@ export class AwsOrganizationWriter {
await this.updateAccount(resource, result.AccountId);
await partitionWriter.updateAccount(resource, result.GovCloudAccountId);
} catch (err) {
if (err.code === 'AccessDenied' && retryCountAccessDenied < 3) {
if (err.name === 'AccessDenied' && retryCountAccessDenied < 3) {
shouldRetry = true;
retryCountAccessDenied = retryCountAccessDenied + 1;
await sleep(3000);
Expand Down Expand Up @@ -413,7 +413,7 @@ export class AwsOrganizationWriter {
});
await iam.send(deleteAccountAliasCommand);
} catch (err) {
if (err && err.code !== 'NoSuchEntity') {
if (err && err.name !== 'NoSuchEntity') {
throw err;
}
}
Expand All @@ -430,7 +430,7 @@ export class AwsOrganizationWriter {
if (current.AccountAliases.find(x => x === alias)) {
return;
}
if (err && err.code === 'EntityAlreadyExists') {
if (err && err.name === 'EntityAlreadyExists') {
throw new OrgFormationError(`The account alias ${alias} already exists. Most likely someone else already registered this alias to some other account.`);
}
}
Expand Down Expand Up @@ -484,7 +484,7 @@ export class AwsOrganizationWriter {
try {
await iam.send(new IAM.DeleteAccountPasswordPolicyCommand({}));
} catch (err) {
if (err && err.code !== 'NoSuchEntity') {
if (err && err.name !== 'NoSuchEntity') {
throw err;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/aws-provider/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export const performAndRetryIfNeeded = async <T extends unknown>(fn: () => Promi
try {
return await fn();
} catch (err) {
if (err && (err.code === 'ConcurrentModificationException' || err.code === 'TooManyRequestsException') && retryCount < 30) {
if (err && (err.name === 'ConcurrentModificationException' || err.name === 'TooManyRequestsException') && retryCount < 30) {
retryCount = retryCount + 1;
shouldRetry = true;
const wait = retryCount + (0.5 * Math.random());
ConsoleUtil.LogDebug(`received retryable error ${err.code}. wait ${wait} and retry-count ${retryCount}`);
ConsoleUtil.LogDebug(`received retryable error ${err.name}. wait ${wait} and retry-count ${retryCount}`);
await sleep(wait * 1000);
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions src/cfn-binder/cfn-task-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class CfnTaskProvider {
customViaRoleArn: binding.customViaRoleArn,
});
} catch (err) {
if (err.code !== 'OptInRequired') {
if (err.name !== 'OptInRequired') {
ConsoleUtil.LogError(`error updating CloudFormation stack ${stackName} in account ${binding.accountId} (${binding.region}). \n${err.message}`);
}
try {
Expand Down Expand Up @@ -269,11 +269,11 @@ export const performAndRetryIfNeeded = async <T extends unknown>(fn: () => Promi
try {
return await fn();
} catch (err) {
if (err && (err.code === 'ThrottlingException') && retryCount < 10) {
if (err && (err.name === 'ThrottlingException') && retryCount < 10) {
retryCount = retryCount + 1;
shouldRetry = true;
const wait = retryCount + (0.5 * Math.random());
ConsoleUtil.LogDebug(`received retryable error ${err.code}. wait ${wait} and retry-count ${retryCount}`);
ConsoleUtil.LogDebug(`received retryable error ${err.name}. wait ${wait} and retry-count ${retryCount}`);
await sleep(wait * 1000);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cfn-binder/cfn-validate-task-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class CfnValidateTaskProvider {
throw new OrgFormationError(`template expects parameter(s) ${missingParameters.join(', ')} which have not been provided`);
}
} catch (err) {
if (err.code === 'AccessDenied') {
if (err.name === 'AccessDenied') {
ConsoleUtil.LogWarning(`access denied when running validate stack: ${err}`);
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export abstract class BaseCliCommand<T extends ICommandArgs> {
command.state = state;
return state;
} catch (err) {
if (err && err.code === 'NoSuchBucket') {
if (err && err.name === 'NoSuchBucket') {
throw new OrgFormationError(`unable to load previously committed state, reason: bucket '${storageProvider.bucketName}' does not exist in current account.`);
}
throw err;
Expand All @@ -133,8 +133,8 @@ export abstract class BaseCliCommand<T extends ICommandArgs> {
if (err instanceof OrgFormationError) {
ConsoleUtil.LogError(err.message);
} else {
if (err.code && err.requestId) {
ConsoleUtil.LogError(`error: ${err.code}, aws-request-id: ${err.requestId}`);
if (err.name && err.requestId) {
ConsoleUtil.LogError(`error: ${err.name}, aws-request-id: ${err.requestId}`);
ConsoleUtil.LogError(err.message);

} else {
Expand Down Expand Up @@ -202,7 +202,7 @@ export abstract class BaseCliCommand<T extends ICommandArgs> {
try {
await storageProvider.create(region);
} catch (err) {
if (err && err.code === 'BucketAlreadyOwnedByYou') {
if (err && err.name === 'BucketAlreadyOwnedByYou') {
return storageProvider;
}
throw err;
Expand Down
2 changes: 1 addition & 1 deletion src/core/generic-task-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class GenericTaskRunner {
task.running = false;
ConsoleUtil.LogInfo(`${delegate.getName(task)} ${delegate.getVerb(task)} successful.`);
} catch (err) {
if ((err.code === 'Throttling' || err.code === 'OptInRequired') && retryAttemptRateLimited < 5) {
if ((err.name === 'Throttling' || err.name === 'OptInRequired') && retryAttemptRateLimited < 5) {
retryWhenRateLimited = true;
retryAttemptRateLimited = retryAttemptRateLimited + 1;
await sleep(Math.pow(retryAttemptRateLimited, 2) + Math.random());
Expand Down
16 changes: 8 additions & 8 deletions src/state/storage-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class S3StorageProvider implements IStorageProvider {

const s3client = AwsUtil.GetS3Service(undefined, region);
try {
await s3client.send(new S3.CreateBucketCommand(request));
await s3client.send(new S3.CreateBucketCommand(request));
await s3client.send(new S3.PutPublicAccessBlockCommand({
Bucket: this.bucketName, PublicAccessBlockConfiguration: {
BlockPublicAcls: true,
Expand All @@ -66,13 +66,13 @@ export class S3StorageProvider implements IStorageProvider {
},
}));
} catch (err) {
if (err && err.code === 'IllegalLocationConstraintException') {
if (err && err.name === 'IllegalLocationConstraintException') {
throw new OrgFormationError(`Unable to create bucket in region ${region}. Is the region spelled correctly?\nIf a bucket with the same name was recently deleted from a different region it could take up to a couple of hours for you to be able to create the same bucket in a different region.`);
}
if (err && err.code === 'BucketAlreadyOwnedByYou') {
if (err && err.name === 'BucketAlreadyOwnedByYou') {
return;
}
if (err && !throwOnAccessDenied && err.code === 'AccessDenied') {
if (err && !throwOnAccessDenied && err.name === 'AccessDenied') {
return; // assume bucket has been set up properly
}
throw err;
Expand All @@ -98,16 +98,16 @@ export class S3StorageProvider implements IStorageProvider {
try {
const response = await s3client.send(new S3.GetObjectCommand(request));
if (!response.Body) { return undefined; }
const contents = response.Body.transformToString();
const contents = await response.Body.transformToString('utf-8');
return contents;
} catch (err) {
if (err && err.code === 'NoSuchKey') {
if (err && err.name === 'NoSuchKey') {
return undefined;
}
if (err && err.code === 'NoSuchBucket') {
if (err && err.name === 'NoSuchBucket') {
return undefined;
}
throw err;
throw err;
}

}
Expand Down
Loading

0 comments on commit 73c45b2

Please sign in to comment.