Skip to content

Commit d1c548e

Browse files
authored
fix(util-dynamodb): state options.wrapNumbers on BigInt error in unmarshall (#2015)
1 parent 1d9a469 commit d1c548e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Diff for: packages/util-dynamodb/src/convertToNative.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ describe("convertToNative", () => {
8686
);
8787
});
8888
});
89+
90+
[`${Number.MAX_SAFE_INTEGER + 2}.1`, `${Number.MIN_SAFE_INTEGER - 2}.1`].forEach((numString) => {
91+
it(`throws if number cannot be converted into BigInt: ${numString}`, () => {
92+
expect(() => {
93+
convertToNative({ N: numString });
94+
}).toThrowError(`${numString} can't be converted to BigInt. Set options.wrapNumbers to get string value.`);
95+
});
96+
});
8997
});
9098

9199
describe("binary", () => {

Diff for: packages/util-dynamodb/src/convertToNative.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ const convertNumber = (numString: string, options?: unmarshallOptions): number |
5050
const infinityValues = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
5151
if ((num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) && !infinityValues.includes(num)) {
5252
if (typeof BigInt === "function") {
53-
return BigInt(numString);
53+
try {
54+
return BigInt(numString);
55+
} catch (error) {
56+
throw new Error(`${numString} can't be converted to BigInt. Set options.wrapNumbers to get string value.`);
57+
}
5458
} else {
5559
throw new Error(`${numString} is outside SAFE_INTEGER bounds. Set options.wrapNumbers to get string value.`);
5660
}

0 commit comments

Comments
 (0)