From 23f844d8715884730179968ea79be5cb4f7b8522 Mon Sep 17 00:00:00 2001 From: Matt Visnovsky Date: Fri, 7 Jun 2024 16:35:06 -0600 Subject: [PATCH] Error handling robustness Cleanup of some things I missed yesterday... --- src/pages/EditMonitor.vue | 2 +- src/util.js | 68 +++++++++++++++---------------- src/util.ts | 85 +++++++++++++++++++-------------------- 3 files changed, 77 insertions(+), 78 deletions(-) diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 5f64c4c9..ce4b2300 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -301,7 +301,7 @@
- +
diff --git a/src/util.js b/src/util.js index 0a576a9a..ca09bdbc 100644 --- a/src/util.js +++ b/src/util.js @@ -405,43 +405,43 @@ async function evaluateJsonQuery(data, jsonPath, jsonPathOperator, expectedValue catch (_a) { response = typeof data === "number" || typeof data === "object" ? data : data.toString(); } - if (jsonPath && typeof data === "object") { - try { - response = await jsonata(jsonPath).evaluate(response); + try { + response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : 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) { - throw new Error(`Error evaluating JSON query: ${err.message}`); + 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, + response + }; } - 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}`); + catch (err) { + throw new Error(`Error evaluating JSON query: ${err.message}. Response from server was: ${response}`); } - 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; diff --git a/src/util.ts b/src/util.ts index 94765f18..751424db 100644 --- a/src/util.ts +++ b/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(); } - // If a JSON path is provided, pre-evaluate the data using it. - if (jsonPath && typeof data === "object") { - try { - response = await jsonata(jsonPath).evaluate(response); - } catch (err: any) { - throw new Error(`Error evaluating JSON query: ${err.message}`); + try { + // If a JSON path is provided, pre-evaluate the data using it. + response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response; + + // Perform the comparison logic using the chosen operator + 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 - }; }