From 078c5c6279ba443098c0e6c8b77f746c9bf5257b Mon Sep 17 00:00:00 2001 From: Ingrid Fielker <ifielker@google.com> Date: Wed, 2 Apr 2025 13:27:17 -0400 Subject: [PATCH 1/4] fix(js/plugins/express): Check request.body is defined before reading data from it. --- js/plugins/express/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/plugins/express/src/index.ts b/js/plugins/express/src/index.ts index aa29ce9fc2..e9abd046e2 100644 --- a/js/plugins/express/src/index.ts +++ b/js/plugins/express/src/index.ts @@ -54,7 +54,7 @@ export function expressHandler< response: express.Response ): Promise<void> => { const { stream } = request.query; - let input = request.body.data as z.infer<I>; + let input = request.body?.data as z.infer<I>; let context: Record<string, any>; try { From c7c1321129dc9ad311d9d231b005181304912231 Mon Sep 17 00:00:00 2001 From: Ingrid Fielker <ifielker@google.com> Date: Thu, 3 Apr 2025 14:11:02 -0400 Subject: [PATCH 2/4] fix(js/plugins/express): Error message if request.body is not defined --- js/plugins/express/src/index.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/js/plugins/express/src/index.ts b/js/plugins/express/src/index.ts index e9abd046e2..3651bee4e6 100644 --- a/js/plugins/express/src/index.ts +++ b/js/plugins/express/src/index.ts @@ -54,7 +54,20 @@ export function expressHandler< response: express.Response ): Promise<void> => { const { stream } = request.query; - let input = request.body?.data as z.infer<I>; + if (!request.body) { + const errMsg = + `Error: request.body is undefined. ` + + `Are you missing 'content-type: application/json' in your headers? ` + + `Or did you forget to use 'express.json()'? `; + logger.error(errMsg); + response + .status(400) + .json({ message: errMsg, status: 'BAD REQUEST' }) + .end(); + return; + } + + let input = request.body.data as z.infer<I>; let context: Record<string, any>; try { From 243cca5d8e8cee7d7e1d413c751ebb975166bdf0 Mon Sep 17 00:00:00 2001 From: Ingrid Fielker <ifielker@google.com> Date: Thu, 3 Apr 2025 16:17:17 -0400 Subject: [PATCH 3/4] fix(js/plugins/express): Modified error message --- js/plugins/express/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/plugins/express/src/index.ts b/js/plugins/express/src/index.ts index 3651bee4e6..38489e2e5c 100644 --- a/js/plugins/express/src/index.ts +++ b/js/plugins/express/src/index.ts @@ -57,8 +57,8 @@ export function expressHandler< if (!request.body) { const errMsg = `Error: request.body is undefined. ` + - `Are you missing 'content-type: application/json' in your headers? ` + - `Or did you forget to use 'express.json()'? `; + `Possible reasons: missing 'content-type: application/json' in request ` + + `headers or misconfigured JSON middleware ('app.use(express.json()')? `; logger.error(errMsg); response .status(400) From c501f6cddc400bdc038efe53f928086a3c9ac147 Mon Sep 17 00:00:00 2001 From: Ingrid Fielker <ifielker@google.com> Date: Thu, 3 Apr 2025 17:04:49 -0400 Subject: [PATCH 4/4] Switch from BAD REQUEST to INVALID ARGUMENT --- js/plugins/express/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/plugins/express/src/index.ts b/js/plugins/express/src/index.ts index 38489e2e5c..08bcadd026 100644 --- a/js/plugins/express/src/index.ts +++ b/js/plugins/express/src/index.ts @@ -62,7 +62,7 @@ export function expressHandler< logger.error(errMsg); response .status(400) - .json({ message: errMsg, status: 'BAD REQUEST' }) + .json({ message: errMsg, status: 'INVALID ARGUMENT' }) .end(); return; }