mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-12-10 00:30:23 -06:00
Fix: improve error messages when passing arrays in evaluateJsonQuery (#6468)
Co-authored-by: Frank Elsinga <frank@elsinga.de>
This commit is contained in:
parent
2135adfed5
commit
fd7435fa51
12
src/util.js
12
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`);
|
||||
}
|
||||
|
||||
10
src/util.ts
10
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`);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user