Skip to content

Commit 563b51e

Browse files
authored
Transform checks for error.code to error.name in promise failure callbacks (#812)
1 parent 54dc048 commit 563b51e

File tree

10 files changed

+184
-16
lines changed

10 files changed

+184
-16
lines changed

.changeset/new-meals-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Transform checks for error.code to error.name in promise failure callbacks

src/transforms/v2-to-v3/__fixtures__/aws-error-name/global-import.input.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,19 @@ client
4040
// Handle other error.
4141
}
4242
});
43+
44+
client
45+
.createBucket({ Bucket })
46+
.promise()
47+
.then(
48+
(response) => {
49+
// Consume the response
50+
},
51+
(error) => {
52+
if (error.code === "BucketAlreadyExists") {
53+
// Handle BucketAlreadyExists error
54+
} else {
55+
// Handle other error.
56+
}
57+
}
58+
);

src/transforms/v2-to-v3/__fixtures__/aws-error-name/global-import.input.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,22 @@ export const funcPromiseCatchFn = async (client: AWS.S3) => {
4444
// Handle other error.
4545
}
4646
});
47-
}
47+
}
48+
49+
export const funcPromiseCatchCallback = async (client: AWS.S3) => {
50+
client
51+
.createBucket({ Bucket })
52+
.promise()
53+
.then(
54+
(response) => {
55+
// Consume the response
56+
},
57+
(error) => {
58+
if (error.code === "BucketAlreadyExists") {
59+
// Handle BucketAlreadyExists error
60+
} else {
61+
// Handle other error.
62+
}
63+
}
64+
);
65+
}

src/transforms/v2-to-v3/__fixtures__/aws-error-name/global-import.output.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,19 @@ client
3737
} else {
3838
// Handle other error.
3939
}
40-
});
40+
});
41+
42+
client
43+
.createBucket({ Bucket })
44+
.then(
45+
(response) => {
46+
// Consume the response
47+
},
48+
(error) => {
49+
if (error.name === "BucketAlreadyExists") {
50+
// Handle BucketAlreadyExists error
51+
} else {
52+
// Handle other error.
53+
}
54+
}
55+
);

src/transforms/v2-to-v3/__fixtures__/aws-error-name/global-import.output.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ export const funcPromiseCatchFn = async (client: S3) => {
4242
// Handle other error.
4343
}
4444
});
45-
}
45+
}
46+
47+
export const funcPromiseCatchCallback = async (client: S3) => {
48+
client
49+
.createBucket({ Bucket })
50+
.then(
51+
(response) => {
52+
// Consume the response
53+
},
54+
(error) => {
55+
if (error.name === "BucketAlreadyExists") {
56+
// Handle BucketAlreadyExists error
57+
} else {
58+
// Handle other error.
59+
}
60+
}
61+
);
62+
}

src/transforms/v2-to-v3/__fixtures__/aws-error-name/service-import.input.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,19 @@ client
4040
// Handle other error.
4141
}
4242
});
43+
44+
client
45+
.createBucket({ Bucket })
46+
.promise()
47+
.then(
48+
(response) => {
49+
// Consume the response
50+
},
51+
(error) => {
52+
if (error.code === "BucketAlreadyExists") {
53+
// Handle BucketAlreadyExists error
54+
} else {
55+
// Handle other error.
56+
}
57+
}
58+
);

src/transforms/v2-to-v3/__fixtures__/aws-error-name/service-import.input.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,22 @@ export const funcPromiseCatchFn = async (client: S3) => {
4444
// Handle other error.
4545
}
4646
});
47-
}
47+
}
48+
49+
export const funcPromiseCatchCallback = async (client: S3) => {
50+
client
51+
.createBucket({ Bucket })
52+
.promise()
53+
.then(
54+
(response) => {
55+
// Consume the response
56+
},
57+
(error) => {
58+
if (error.code === "BucketAlreadyExists") {
59+
// Handle BucketAlreadyExists error
60+
} else {
61+
// Handle other error.
62+
}
63+
}
64+
);
65+
}

src/transforms/v2-to-v3/__fixtures__/aws-error-name/service-import.output.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,20 @@ client
3737
} else {
3838
// Handle other error.
3939
}
40-
});
40+
});
41+
42+
client
43+
.createBucket({ Bucket })
44+
.then(
45+
(response) => {
46+
// Consume the response
47+
},
48+
(error) => {
49+
if (error.name === "BucketAlreadyExists") {
50+
// Handle BucketAlreadyExists error
51+
} else {
52+
// Handle other error.
53+
}
54+
}
55+
);
56+

src/transforms/v2-to-v3/__fixtures__/aws-error-name/service-import.output.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ export const funcPromiseCatchFn = async (client: S3) => {
4242
// Handle other error.
4343
}
4444
});
45-
}
45+
}
46+
47+
export const funcPromiseCatchCallback = async (client: S3) => {
48+
client
49+
.createBucket({ Bucket })
50+
.then(
51+
(response) => {
52+
// Consume the response
53+
},
54+
(error) => {
55+
if (error.name === "BucketAlreadyExists") {
56+
// Handle BucketAlreadyExists error
57+
} else {
58+
// Handle other error.
59+
}
60+
}
61+
);
62+
}

src/transforms/v2-to-v3/apis/renameErrorCodeWithName.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010

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

13+
const FUNCTION_EXPRESSION_TYPES = ["ArrowFunctionExpression", "FunctionExpression"];
14+
1315
const renameCodeWithName = (
1416
j: JSCodeshift,
1517
source: Collection<unknown>,
@@ -23,9 +25,10 @@ const renameCodeWithName = (
2325
.replaceWith(() => j.memberExpression(j.identifier(errorName), j.identifier("name")));
2426
};
2527

26-
const getCatchExpression = (
28+
const getFirstExpression = (
2729
j: JSCodeshift,
28-
callExpression: ASTPath<CallExpression>
30+
callExpression: ASTPath<CallExpression>,
31+
expressionName: string
2932
): ASTPath<CallExpression> | null => {
3033
const parentPath = callExpression.parentPath.value;
3134
if (parentPath?.type !== "MemberExpression") {
@@ -37,10 +40,10 @@ const getCatchExpression = (
3740
return null;
3841
}
3942

40-
if (parentPath.property.type === "Identifier" && parentPath.property.name === "catch") {
43+
if (parentPath.property.type === "Identifier" && parentPath.property.name === expressionName) {
4144
return callExpression.parentPath.parentPath;
4245
}
43-
return getCatchExpression(j, callExpression.parentPath.parentPath);
46+
return getFirstExpression(j, callExpression.parentPath.parentPath, expressionName);
4447
};
4548

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

7578
// Replace error.code with error.name in promise catch.
7679
callExpressions
77-
.map((callExpression) => getCatchExpression(j, callExpression))
80+
.map((callExpression) => getFirstExpression(j, callExpression, "catch"))
7881
.forEach((catchExpression) => {
7982
if (!catchExpression) {
8083
return;
8184
}
8285

83-
if (
84-
!["ArrowFunctionExpression", "FunctionExpression"].includes(
85-
catchExpression.value.arguments[0].type
86-
)
87-
) {
86+
if (!FUNCTION_EXPRESSION_TYPES.includes(catchExpression.value.arguments[0].type)) {
8887
return;
8988
}
9089

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

10099
renameCodeWithName(j, j(catchFunction.body), errorParam.name);
101100
});
101+
102+
// Replace error.code with error.name in promise failure callback.
103+
callExpressions
104+
.map((callExpression) => getFirstExpression(j, callExpression, "then"))
105+
.forEach((thenExpression) => {
106+
if (!thenExpression) {
107+
return;
108+
}
109+
110+
if (thenExpression.value.arguments.length !== 2) {
111+
return;
112+
}
113+
114+
if (
115+
!FUNCTION_EXPRESSION_TYPES.includes(thenExpression.value.arguments[0].type) ||
116+
!FUNCTION_EXPRESSION_TYPES.includes(thenExpression.value.arguments[1].type)
117+
) {
118+
return;
119+
}
120+
121+
const failureCallbackFunction = thenExpression.value.arguments[1] as
122+
| FunctionExpression
123+
| ArrowFunctionExpression;
124+
const errorParam = failureCallbackFunction.params[0];
125+
126+
if (errorParam?.type !== "Identifier") {
127+
return;
128+
}
129+
130+
renameCodeWithName(j, j(failureCallbackFunction.body), errorParam.name);
131+
});
102132
}
103133
};

0 commit comments

Comments
 (0)