Skip to content

Commit

Permalink
Transform checks for error.code to error.name in try-catch (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Mar 25, 2024
1 parent 9e3f82c commit 8178f56
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/poor-poems-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Transform checks for error.code to error.name in try-catch
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import AWS from "aws-sdk";

const client = new AWS.S3();

try {
await client.createBucket({
Bucket: "bucket"
}).promise();
} catch (error) {
if (error.code === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
throw error;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import AWS from "aws-sdk";

export const func = async (client: AWS.S3) => {
try {
await client.createBucket({
Bucket: "bucket"
}).promise();
} catch (error) {
if (error.code === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
throw error;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { S3 } from "@aws-sdk/client-s3";

const client = new S3();

try {
await client.createBucket({
Bucket: "bucket"
});
} catch (error) {
if (error.name === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
throw error;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { S3 } from "@aws-sdk/client-s3";

export const func = async (client: S3) => {
try {
await client.createBucket({
Bucket: "bucket"
});
} catch (error) {
if (error.name === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
throw error;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { S3 } from "aws-sdk";

const client = new S3();

try {
await client.createBucket({
Bucket: "bucket"
}).promise();
} catch (error) {
if (error.code === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
throw error;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { S3 } from "aws-sdk";

export const func = async (client: S3) => {
try {
await client.createBucket({
Bucket: "bucket"
}).promise();
} catch (error) {
if (error.code === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
throw error;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { S3 } from "@aws-sdk/client-s3";

const client = new S3();

try {
await client.createBucket({
Bucket: "bucket"
});
} catch (error) {
if (error.name === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
throw error;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { S3 } from "@aws-sdk/client-s3";

export const func = async (client: S3) => {
try {
await client.createBucket({
Bucket: "bucket"
});
} catch (error) {
if (error.name === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
throw error;
}
}
}
1 change: 0 additions & 1 deletion src/transforms/v2-to-v3/apis/addEmptyObjectForUndefined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export const addEmptyObjectForUndefined = (
clientIdentifiers: ClientIdentifier[]
): void => {
for (const clientId of clientIdentifiers) {
// Remove .promise() from client API calls.
source
.find(j.CallExpression, {
callee: {
Expand Down
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from "./replaceS3UploadApi";
export * from "./replaceWaiterApi";
export * from "./getCommandName";
export * from "./getClientIdentifiersRecord";
export * from "./renameErrorCodeWithName";
48 changes: 48 additions & 0 deletions src/transforms/v2-to-v3/apis/renameErrorCodeWithName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Collection, JSCodeshift, TryStatement } from "jscodeshift";

import { ClientIdentifier } from "../types";

// Renames error.code with error.name.
export const renameErrorCodeWithName = (
j: JSCodeshift,
source: Collection<unknown>,
clientIdentifiers: ClientIdentifier[]
): void => {
for (const clientId of clientIdentifiers) {
// Replace error.code with error.name in catch clauses.
source
.find(j.CallExpression, {
callee: {
type: "MemberExpression",
object: clientId,
},
})
.forEach((callExpression) => {
const tryStatement = j(callExpression).closest(j.TryStatement).nodes()[0] as TryStatement;

if (!tryStatement || !tryStatement.handler) {
return;
}

const catchClause = tryStatement.handler;
const errorParam = catchClause.param;

if (errorParam?.type !== "Identifier") {
return;
}

j(catchClause.body)
.find(j.MemberExpression, {
object: {
type: "Identifier",
name: errorParam.name,
},
property: {
type: "Identifier",
name: "code",
},
})
.replaceWith(() => j.memberExpression(errorParam, j.identifier("name")));
});
}
};
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
replaceAwsIdentity,
replaceAwsError,
addEmptyObjectForUndefined,
renameErrorCodeWithName,
} from "./apis";
import { replaceAwsUtilFunctions } from "./aws-util";
import {
Expand Down Expand Up @@ -99,6 +100,7 @@ const transformer = async (file: FileInfo, api: API) => {
}

removePromiseCalls(j, source, clientIdentifiers);
renameErrorCodeWithName(j, source, clientIdentifiers);
addEmptyObjectForUndefined(j, source, clientIdentifiers);

if (v2ClientName === S3) {
Expand Down

0 comments on commit 8178f56

Please sign in to comment.