-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform checks for error.code to error.name in try-catch (#808)
- Loading branch information
Showing
13 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/aws-error-name/global-import.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/aws-error-name/global-import.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/aws-error-name/global-import.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/aws-error-name/global-import.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/aws-error-name/service-import.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/aws-error-name/service-import.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/aws-error-name/service-import.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/aws-error-name/service-import.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters