From fd7435fa518fc87709818872947e7b68ae6631f5 Mon Sep 17 00:00:00 2001 From: Matt Visnovsky Date: Tue, 9 Dec 2025 15:08:17 -0700 Subject: [PATCH] Fix: improve error messages when passing arrays in evaluateJsonQuery (#6468) Co-authored-by: Frank Elsinga --- src/util.js | 12 +++++++----- src/util.ts | 10 ++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/util.js b/src/util.js index 9ceaaf06..e6b77d18 100644 --- a/src/util.js +++ b/src/util.js @@ -227,11 +227,7 @@ class Logger { this.log(module, "debug", ...msg); } exception(module, exception, ...msg) { - let finalMessage = exception; - if (msg) { - finalMessage = `${msg}: ${exception}`; - } - this.log(module, "error", finalMessage); + this.log(module, "error", ...msg, exception); } } exports.log = new Logger(); @@ -400,6 +396,12 @@ async function evaluateJsonQuery(data, jsonPath, jsonPathOperator, expectedValue if (response === null || response === undefined) { throw new Error("Empty or undefined response. Check query syntax and response structure"); } + if (Array.isArray(response)) { + const responseStr = JSON.stringify(response); + const truncatedResponse = responseStr.length > 25 ? responseStr.substring(0, 25) + "...]" : responseStr; + throw new Error("JSON query returned the array " + truncatedResponse + ", but a primitive value is required. " + + "Modify your query to return a single value via [0] to get the first element or use an aggregation like $count(), $sum() or $boolean()."); + } if (typeof response === "object" || response instanceof Date || typeof response === "function") { throw new Error(`The post-JSON query evaluated response from the server is of type ${typeof response}, which cannot be directly compared to the expected value`); } diff --git a/src/util.ts b/src/util.ts index 6bf9501b..f689abbe 100644 --- a/src/util.ts +++ b/src/util.ts @@ -674,6 +674,16 @@ export async function evaluateJsonQuery(data: any, jsonPath: string, jsonPathOpe throw new Error("Empty or undefined response. Check query syntax and response structure"); } + // Check for arrays: JSONata filter expressions like .[predicate] always return arrays + if (Array.isArray(response)) { + const responseStr = JSON.stringify(response); + const truncatedResponse = responseStr.length > 25 ? responseStr.substring(0, 25) + "...]" : responseStr; + throw new Error( + "JSON query returned the array " + truncatedResponse + ", but a primitive value is required. " + + "Modify your query to return a single value via [0] to get the first element or use an aggregation like $count(), $sum() or $boolean()." + ); + } + if (typeof response === "object" || response instanceof Date || typeof response === "function") { throw new Error(`The post-JSON query evaluated response from the server is of type ${typeof response}, which cannot be directly compared to the expected value`); }