Skip to content

Commit b8ae45d

Browse files
authored
Transform AWSError to ServiceException (#776)
1 parent f96306a commit b8ae45d

File tree

10 files changed

+105
-0
lines changed

10 files changed

+105
-0
lines changed

.changeset/hungry-foxes-bow.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": minor
3+
---
4+
5+
Transform AWSError to ServiceException
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import AWS = require("aws-sdk");
2+
3+
export const isAwsError = (error: unknown): error is AWS.AWSError => {
4+
if (error === undefined) {
5+
return false
6+
}
7+
return error instanceof Error;
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import AWS_smithy_client = require("@smithy/smithy-client");
2+
import ServiceException = AWS_smithy_client.ServiceException;
3+
4+
export const isAwsError = (error: unknown): error is ServiceException => {
5+
if (error === undefined) {
6+
return false
7+
}
8+
return error instanceof Error;
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import AWS from "aws-sdk";
2+
3+
export const isAwsError = (error: unknown): error is AWS.AWSError => {
4+
if (error === undefined) {
5+
return false
6+
}
7+
return error instanceof Error;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ServiceException } from "@smithy/smithy-client";
2+
3+
export const isAwsError = (error: unknown): error is ServiceException => {
4+
if (error === undefined) {
5+
return false
6+
}
7+
return error instanceof Error;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { AWSError } from "aws-sdk";
2+
3+
export const isAwsError = (error: unknown): error is AWSError => {
4+
if (error === undefined) {
5+
return false
6+
}
7+
return error instanceof Error;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ServiceException } from "@smithy/smithy-client";
2+
3+
export const isAwsError = (error: unknown): error is ServiceException => {
4+
if (error === undefined) {
5+
return false
6+
}
7+
return error instanceof Error;
8+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from "./isS3GetSignedUrlApiUsed";
77
export * from "./isS3UploadApiUsed";
88
export * from "./removePromiseCalls";
99
export * from "./replaceAwsIdentity";
10+
export * from "./replaceAwsError";
1011
export * from "./replaceS3GetSignedUrlApi";
1112
export * from "./replaceS3UploadApi";
1213
export * from "./replaceWaiterApi";
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { ImportType, addNamedModule } from "../modules";
3+
4+
export interface ReplaceAwsErrorOptions {
5+
v2GlobalName?: string;
6+
importType: ImportType;
7+
}
8+
9+
export const replaceAwsError = (
10+
j: JSCodeshift,
11+
source: Collection<unknown>,
12+
{ v2GlobalName, importType }: ReplaceAwsErrorOptions
13+
) => {
14+
const v2AwsErrorName = "AWSError";
15+
const v3AwsErrorName = "ServiceException";
16+
17+
const namedModuleParams = {
18+
importType,
19+
localName: v3AwsErrorName,
20+
packageName: "@smithy/smithy-client",
21+
};
22+
23+
if (v2GlobalName) {
24+
source
25+
.find(j.TSTypeReference, {
26+
typeName: {
27+
left: { type: "Identifier", name: v2GlobalName },
28+
right: { type: "Identifier", name: v2AwsErrorName },
29+
},
30+
})
31+
.replaceWith((v2ErrorType) => {
32+
addNamedModule(j, source, namedModuleParams);
33+
return j.tsTypeReference(j.identifier(v3AwsErrorName), v2ErrorType.node.typeParameters);
34+
});
35+
}
36+
37+
source
38+
.find(j.TSTypeReference, {
39+
typeName: {
40+
type: "Identifier",
41+
name: v2AwsErrorName,
42+
},
43+
})
44+
.replaceWith((v2ErrorType) => {
45+
addNamedModule(j, source, namedModuleParams);
46+
return j.tsTypeReference(j.identifier(v3AwsErrorName), v2ErrorType.node.typeParameters);
47+
});
48+
};

src/transforms/v2-to-v3/transformer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
replaceS3GetSignedUrlApi,
1010
getClientIdentifiersRecord,
1111
replaceAwsIdentity,
12+
replaceAwsError,
1213
} from "./apis";
1314
import { replaceAwsUtilFunctions } from "./aws-util";
1415
import {
@@ -110,6 +111,7 @@ const transformer = async (file: FileInfo, api: API) => {
110111
replaceAwsConfig(j, source, { v2GlobalName, awsGlobalConfig });
111112
replaceAwsIdentity(j, source, { v2GlobalName, importType });
112113
replaceAwsUtilFunctions(j, source, v2GlobalName);
114+
replaceAwsError(j, source, { v2GlobalName, importType });
113115
removeModules(j, source, importType);
114116

115117
const sourceString = getFormattedSourceString(source.toSource({ quote, useTabs, trailingComma }));

0 commit comments

Comments
 (0)