mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-23 14:54:05 +00:00
Error handling robustness
Cleanup of some things I missed yesterday...
This commit is contained in:
parent
fdc145bffd
commit
23f844d871
3 changed files with 77 additions and 78 deletions
|
@ -301,7 +301,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="flex-grow-1">
|
<div class="flex-grow-1">
|
||||||
<label for="expectedValue" class="form-label">{{ $t("Expected Value") }}</label>
|
<label for="expectedValue" class="form-label">{{ $t("Expected Value") }}</label>
|
||||||
<input v-if="monitor.jsonPathOperator !== 'contains' && monitor.jsonPathOperator !== '=='" id="expectedValue" v-model="monitor.expectedValue" type="number" class="form-control" required step=".01">
|
<input v-if="monitor.jsonPathOperator !== 'contains' && monitor.jsonPathOperator !== '==' && monitor.jsonPathOperator !== '!='" id="expectedValue" v-model="monitor.expectedValue" type="number" class="form-control" required step=".01">
|
||||||
<input v-else id="expectedValue" v-model="monitor.expectedValue" type="text" class="form-control" required>
|
<input v-else id="expectedValue" v-model="monitor.expectedValue" type="text" class="form-control" required>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
68
src/util.js
68
src/util.js
|
@ -405,43 +405,43 @@ async function evaluateJsonQuery(data, jsonPath, jsonPathOperator, expectedValue
|
||||||
catch (_a) {
|
catch (_a) {
|
||||||
response = typeof data === "number" || typeof data === "object" ? data : data.toString();
|
response = typeof data === "number" || typeof data === "object" ? data : data.toString();
|
||||||
}
|
}
|
||||||
if (jsonPath && typeof data === "object") {
|
try {
|
||||||
try {
|
response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response;
|
||||||
response = await jsonata(jsonPath).evaluate(response);
|
let jsonQueryExpression;
|
||||||
|
switch (jsonPathOperator) {
|
||||||
|
case ">":
|
||||||
|
case ">=":
|
||||||
|
case "<":
|
||||||
|
case "<=":
|
||||||
|
jsonQueryExpression = `$.value ${jsonPathOperator} $.expected`;
|
||||||
|
break;
|
||||||
|
case "!=":
|
||||||
|
jsonQueryExpression = "$string($.value) != $string($.expected)";
|
||||||
|
break;
|
||||||
|
case "==":
|
||||||
|
jsonQueryExpression = "$string($.value) = $string($.expected)";
|
||||||
|
break;
|
||||||
|
case "contains":
|
||||||
|
jsonQueryExpression = "$contains($string($.value), $string($.expected))";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error(`Invalid condition ${jsonPathOperator}`);
|
||||||
}
|
}
|
||||||
catch (err) {
|
const expression = jsonata(jsonQueryExpression);
|
||||||
throw new Error(`Error evaluating JSON query: ${err.message}`);
|
const status = await expression.evaluate({
|
||||||
|
value: response,
|
||||||
|
expected: expectedValue
|
||||||
|
});
|
||||||
|
if (response === undefined || status === undefined) {
|
||||||
|
throw new Error("Query evaluation returned undefined. Check query syntax and the structure of the response data");
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
|
status,
|
||||||
|
response
|
||||||
|
};
|
||||||
}
|
}
|
||||||
let jsonQueryExpression;
|
catch (err) {
|
||||||
switch (jsonPathOperator) {
|
throw new Error(`Error evaluating JSON query: ${err.message}. Response from server was: ${response}`);
|
||||||
case ">":
|
|
||||||
case ">=":
|
|
||||||
case "<":
|
|
||||||
case "<=":
|
|
||||||
case "!=":
|
|
||||||
jsonQueryExpression = `$.value ${jsonPathOperator} $.expected`;
|
|
||||||
break;
|
|
||||||
case "==":
|
|
||||||
jsonQueryExpression = "$string($.value) = $string($.expected)";
|
|
||||||
break;
|
|
||||||
case "contains":
|
|
||||||
jsonQueryExpression = "$contains($string($.value), $string($.expected))";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Error(`Invalid condition ${jsonPathOperator}`);
|
|
||||||
}
|
}
|
||||||
const expression = jsonata(jsonQueryExpression);
|
|
||||||
const status = await expression.evaluate({
|
|
||||||
value: response.toString(),
|
|
||||||
expected: expectedValue.toString()
|
|
||||||
});
|
|
||||||
if (response === undefined || status === undefined) {
|
|
||||||
throw new Error("Query evaluation returned undefined. Check your query syntax and the structure of the response data.");
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
status,
|
|
||||||
response
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
exports.evaluateJsonQuery = evaluateJsonQuery;
|
exports.evaluateJsonQuery = evaluateJsonQuery;
|
||||||
|
|
85
src/util.ts
85
src/util.ts
|
@ -663,49 +663,48 @@ export async function evaluateJsonQuery(data: any, jsonPath: string, jsonPathOpe
|
||||||
response = typeof data === "number" || typeof data === "object" ? data : data.toString();
|
response = typeof data === "number" || typeof data === "object" ? data : data.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a JSON path is provided, pre-evaluate the data using it.
|
try {
|
||||||
if (jsonPath && typeof data === "object") {
|
// If a JSON path is provided, pre-evaluate the data using it.
|
||||||
try {
|
response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response;
|
||||||
response = await jsonata(jsonPath).evaluate(response);
|
|
||||||
} catch (err: any) {
|
// Perform the comparison logic using the chosen operator
|
||||||
throw new Error(`Error evaluating JSON query: ${err.message}`);
|
let jsonQueryExpression;
|
||||||
|
switch (jsonPathOperator) {
|
||||||
|
case ">":
|
||||||
|
case ">=":
|
||||||
|
case "<":
|
||||||
|
case "<=":
|
||||||
|
jsonQueryExpression = `$.value ${jsonPathOperator} $.expected`;
|
||||||
|
break;
|
||||||
|
case "!=":
|
||||||
|
jsonQueryExpression = "$string($.value) != $string($.expected)";
|
||||||
|
break;
|
||||||
|
case "==":
|
||||||
|
jsonQueryExpression = "$string($.value) = $string($.expected)";
|
||||||
|
break;
|
||||||
|
case "contains":
|
||||||
|
jsonQueryExpression = "$contains($string($.value), $string($.expected))";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error(`Invalid condition ${jsonPathOperator}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Evaluate the JSON Query Expression
|
||||||
|
const expression = jsonata(jsonQueryExpression);
|
||||||
|
const status = await expression.evaluate({
|
||||||
|
value: response,
|
||||||
|
expected: expectedValue
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response === undefined || status === undefined) {
|
||||||
|
throw new Error("Query evaluation returned undefined. Check query syntax and the structure of the response data");
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status, // The evaluation of the json query
|
||||||
|
response // The response from the server or result from initial json-query evaluation
|
||||||
|
};
|
||||||
|
} catch (err: any) {
|
||||||
|
throw new Error(`Error evaluating JSON query: ${err.message}. Response from server was: ${response}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform the comparison logic using the chosen operator
|
|
||||||
// Perform the comparison logic using the chosen operator
|
|
||||||
let jsonQueryExpression;
|
|
||||||
switch (jsonPathOperator) {
|
|
||||||
case ">":
|
|
||||||
case ">=":
|
|
||||||
case "<":
|
|
||||||
case "<=":
|
|
||||||
case "!=":
|
|
||||||
jsonQueryExpression = `$.value ${jsonPathOperator} $.expected`;
|
|
||||||
break;
|
|
||||||
case "==":
|
|
||||||
jsonQueryExpression = "$string($.value) = $string($.expected)";
|
|
||||||
break;
|
|
||||||
case "contains":
|
|
||||||
jsonQueryExpression = "$contains($string($.value), $string($.expected))";
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Error(`Invalid condition ${jsonPathOperator}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Evaluate the JSON Query Expression
|
|
||||||
const expression = jsonata(jsonQueryExpression);
|
|
||||||
const status = await expression.evaluate({
|
|
||||||
value: response.toString(),
|
|
||||||
expected: expectedValue.toString()
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response === undefined || status === undefined) {
|
|
||||||
throw new Error("Query evaluation returned undefined. Check your query syntax and the structure of the response data.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
status, // The evaluation of the json query
|
|
||||||
response // The response from the server or result from initial json-query evaluation
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue