Skip to content

Commit

Permalink
fix(util-dynamodb): state options.wrapNumbers on BigInt error in unma…
Browse files Browse the repository at this point in the history
…rshall (#2015)
  • Loading branch information
trivikr committed Feb 10, 2021
1 parent 1d9a469 commit d1c548e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/util-dynamodb/src/convertToNative.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ describe("convertToNative", () => {
);
});
});

[`${Number.MAX_SAFE_INTEGER + 2}.1`, `${Number.MIN_SAFE_INTEGER - 2}.1`].forEach((numString) => {
it(`throws if number cannot be converted into BigInt: ${numString}`, () => {
expect(() => {
convertToNative({ N: numString });
}).toThrowError(`${numString} can't be converted to BigInt. Set options.wrapNumbers to get string value.`);
});
});
});

describe("binary", () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/util-dynamodb/src/convertToNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ const convertNumber = (numString: string, options?: unmarshallOptions): number |
const infinityValues = [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY];
if ((num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) && !infinityValues.includes(num)) {
if (typeof BigInt === "function") {
return BigInt(numString);
try {
return BigInt(numString);
} catch (error) {
throw new Error(`${numString} can't be converted to BigInt. Set options.wrapNumbers to get string value.`);
}
} else {
throw new Error(`${numString} is outside SAFE_INTEGER bounds. Set options.wrapNumbers to get string value.`);
}
Expand Down

0 comments on commit d1c548e

Please sign in to comment.