Fix: improve error messages when passing arrays in evaluateJsonQuery (#6468)

Co-authored-by: Frank Elsinga <frank@elsinga.de>
This commit is contained in:
Matt Visnovsky 2025-12-09 15:08:17 -07:00 committed by GitHub
parent 2135adfed5
commit fd7435fa51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 5 deletions

View File

@ -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`);
}

View File

@ -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`);
}