From 813ff8a08b04a1b9d8bf0d66836d2ea3bcaa4d6a Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Tue, 8 Mar 2022 17:19:12 -0800 Subject: [PATCH] chore(util-body-length): throw Error if body length can't be computed (#3401) --- .../src/calculateBodyLength.spec.ts | 6 ++++++ .../util-body-length-browser/src/calculateBodyLength.ts | 1 + .../util-body-length-node/src/calculateBodyLength.spec.ts | 6 ++++++ packages/util-body-length-node/src/calculateBodyLength.ts | 1 + 4 files changed, 14 insertions(+) diff --git a/packages/util-body-length-browser/src/calculateBodyLength.spec.ts b/packages/util-body-length-browser/src/calculateBodyLength.spec.ts index 99ab3069981a..301ed51a735c 100644 --- a/packages/util-body-length-browser/src/calculateBodyLength.spec.ts +++ b/packages/util-body-length-browser/src/calculateBodyLength.spec.ts @@ -33,4 +33,10 @@ describe(calculateBodyLength.name, () => { }; expect(calculateBodyLength(mockFileObject)).toEqual(mockFileObject.size); }); + + it.each([true, 1, {}, []])("throws error if Body Length computation fails for: %s", (body) => { + expect(() => { + expect(calculateBodyLength(body)); + }).toThrowError(`Body Length computation failed for ${body}`); + }); }); diff --git a/packages/util-body-length-browser/src/calculateBodyLength.ts b/packages/util-body-length-browser/src/calculateBodyLength.ts index e73e1cb83290..55ca7f7fa921 100644 --- a/packages/util-body-length-browser/src/calculateBodyLength.ts +++ b/packages/util-body-length-browser/src/calculateBodyLength.ts @@ -16,4 +16,5 @@ export const calculateBodyLength = (body: any): number | undefined => { // handles browser File object return body.size; } + throw new Error(`Body Length computation failed for ${body}`); }; diff --git a/packages/util-body-length-node/src/calculateBodyLength.spec.ts b/packages/util-body-length-node/src/calculateBodyLength.spec.ts index cb5ae86d13e0..93416c284c13 100644 --- a/packages/util-body-length-node/src/calculateBodyLength.spec.ts +++ b/packages/util-body-length-node/src/calculateBodyLength.spec.ts @@ -61,4 +61,10 @@ describe(calculateBodyLength.name, () => { } }); }); + + it.each([true, 1, {}, []])("throws error if Body Length computation fails for: %s", (body) => { + expect(() => { + expect(calculateBodyLength(body)); + }).toThrowError(`Body Length computation failed for ${body}`); + }); }); diff --git a/packages/util-body-length-node/src/calculateBodyLength.ts b/packages/util-body-length-node/src/calculateBodyLength.ts index 8d3293f821ca..792ce72c0f11 100644 --- a/packages/util-body-length-node/src/calculateBodyLength.ts +++ b/packages/util-body-length-node/src/calculateBodyLength.ts @@ -18,4 +18,5 @@ export const calculateBodyLength = (body: any): number | undefined => { // handles fd readable streams return fstatSync(body.fd).size; } + throw new Error(`Body Length computation failed for ${body}`); };