Skip to content

Commit

Permalink
Transform checks for error.code to error.name in promise failure call…
Browse files Browse the repository at this point in the history
…backs (#812)
  • Loading branch information
trivikr committed Mar 26, 2024
1 parent 54dc048 commit 563b51e
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-meals-sell.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 promise failure callbacks
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ client
// Handle other error.
}
});

client
.createBucket({ Bucket })
.promise()
.then(
(response) => {
// Consume the response
},
(error) => {
if (error.code === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
// Handle other error.
}
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ export const funcPromiseCatchFn = async (client: AWS.S3) => {
// Handle other error.
}
});
}
}

export const funcPromiseCatchCallback = async (client: AWS.S3) => {
client
.createBucket({ Bucket })
.promise()
.then(
(response) => {
// Consume the response
},
(error) => {
if (error.code === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
// Handle other error.
}
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ client
} else {
// Handle other error.
}
});
});

client
.createBucket({ Bucket })
.then(
(response) => {
// Consume the response
},
(error) => {
if (error.name === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
// Handle other error.
}
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@ export const funcPromiseCatchFn = async (client: S3) => {
// Handle other error.
}
});
}
}

export const funcPromiseCatchCallback = async (client: S3) => {
client
.createBucket({ Bucket })
.then(
(response) => {
// Consume the response
},
(error) => {
if (error.name === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
// Handle other error.
}
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ client
// Handle other error.
}
});

client
.createBucket({ Bucket })
.promise()
.then(
(response) => {
// Consume the response
},
(error) => {
if (error.code === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
// Handle other error.
}
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ export const funcPromiseCatchFn = async (client: S3) => {
// Handle other error.
}
});
}
}

export const funcPromiseCatchCallback = async (client: S3) => {
client
.createBucket({ Bucket })
.promise()
.then(
(response) => {
// Consume the response
},
(error) => {
if (error.code === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
// Handle other error.
}
}
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,20 @@ client
} else {
// Handle other error.
}
});
});

client
.createBucket({ Bucket })
.then(
(response) => {
// Consume the response
},
(error) => {
if (error.name === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
// Handle other error.
}
}
);

Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@ export const funcPromiseCatchFn = async (client: S3) => {
// Handle other error.
}
});
}
}

export const funcPromiseCatchCallback = async (client: S3) => {
client
.createBucket({ Bucket })
.then(
(response) => {
// Consume the response
},
(error) => {
if (error.name === "BucketAlreadyExists") {
// Handle BucketAlreadyExists error
} else {
// Handle other error.
}
}
);
}
50 changes: 40 additions & 10 deletions src/transforms/v2-to-v3/apis/renameErrorCodeWithName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {

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

const FUNCTION_EXPRESSION_TYPES = ["ArrowFunctionExpression", "FunctionExpression"];

const renameCodeWithName = (
j: JSCodeshift,
source: Collection<unknown>,
Expand All @@ -23,9 +25,10 @@ const renameCodeWithName = (
.replaceWith(() => j.memberExpression(j.identifier(errorName), j.identifier("name")));
};

const getCatchExpression = (
const getFirstExpression = (
j: JSCodeshift,
callExpression: ASTPath<CallExpression>
callExpression: ASTPath<CallExpression>,
expressionName: string
): ASTPath<CallExpression> | null => {
const parentPath = callExpression.parentPath.value;
if (parentPath?.type !== "MemberExpression") {
Expand All @@ -37,10 +40,10 @@ const getCatchExpression = (
return null;
}

if (parentPath.property.type === "Identifier" && parentPath.property.name === "catch") {
if (parentPath.property.type === "Identifier" && parentPath.property.name === expressionName) {
return callExpression.parentPath.parentPath;
}
return getCatchExpression(j, callExpression.parentPath.parentPath);
return getFirstExpression(j, callExpression.parentPath.parentPath, expressionName);
};

// Renames error.code with error.name.
Expand Down Expand Up @@ -74,17 +77,13 @@ export const renameErrorCodeWithName = (

// Replace error.code with error.name in promise catch.
callExpressions
.map((callExpression) => getCatchExpression(j, callExpression))
.map((callExpression) => getFirstExpression(j, callExpression, "catch"))
.forEach((catchExpression) => {
if (!catchExpression) {
return;
}

if (
!["ArrowFunctionExpression", "FunctionExpression"].includes(
catchExpression.value.arguments[0].type
)
) {
if (!FUNCTION_EXPRESSION_TYPES.includes(catchExpression.value.arguments[0].type)) {
return;
}

Expand All @@ -99,5 +98,36 @@ export const renameErrorCodeWithName = (

renameCodeWithName(j, j(catchFunction.body), errorParam.name);
});

// Replace error.code with error.name in promise failure callback.
callExpressions
.map((callExpression) => getFirstExpression(j, callExpression, "then"))
.forEach((thenExpression) => {
if (!thenExpression) {
return;
}

if (thenExpression.value.arguments.length !== 2) {
return;
}

if (
!FUNCTION_EXPRESSION_TYPES.includes(thenExpression.value.arguments[0].type) ||
!FUNCTION_EXPRESSION_TYPES.includes(thenExpression.value.arguments[1].type)
) {
return;
}

const failureCallbackFunction = thenExpression.value.arguments[1] as
| FunctionExpression
| ArrowFunctionExpression;
const errorParam = failureCallbackFunction.params[0];

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

renameCodeWithName(j, j(failureCallbackFunction.body), errorParam.name);
});
}
};

0 comments on commit 563b51e

Please sign in to comment.