Robustness and edge-case handling

This commit is contained in:
Matt Visnovsky 2024-06-14 16:45:51 -06:00
parent a0374487ce
commit 8d8ce23f2b
2 changed files with 17 additions and 8 deletions

View file

@ -403,11 +403,14 @@ async function evaluateJsonQuery(data, jsonPath, jsonPathOperator, expectedValue
response = JSON.parse(data); response = JSON.parse(data);
} }
catch (_a) { catch (_a) {
response = typeof data === "number" || typeof data === "object" ? data : data.toString(); response = (typeof data === "object" || typeof data === "number") && !Buffer.isBuffer(data) ? data : data.toString();
} }
try { try {
response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response; response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response;
if (typeof response === "object" || Array.isArray(response) || response instanceof Date || typeof response === "function") { if (response === null || response === undefined) {
throw new Error("Empty or undefined response. Check query syntax and response structure");
}
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`); 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`);
} }
let jsonQueryExpression; let jsonQueryExpression;
@ -435,7 +438,7 @@ async function evaluateJsonQuery(data, jsonPath, jsonPathOperator, expectedValue
value: response.toString(), value: response.toString(),
expected: expectedValue.toString() expected: expectedValue.toString()
}); });
if (response === undefined || status === undefined) { if (status === undefined) {
throw new Error("Query evaluation returned undefined. Check query syntax and the structure of the response data"); throw new Error("Query evaluation returned undefined. Check query syntax and the structure of the response data");
} }
return { return {
@ -444,7 +447,8 @@ async function evaluateJsonQuery(data, jsonPath, jsonPathOperator, expectedValue
}; };
} }
catch (err) { catch (err) {
response = (response.toString().length > 50) ? `${response.substring(0, 50)}… (truncated)` : response.toString(); response = JSON.stringify(response);
response = (response && response.length > 50) ? `${response.substring(0, 100)}… (truncated)` : response;
throw new Error(`Error evaluating JSON query: ${err.message}. Response from server was: ${response}`); throw new Error(`Error evaluating JSON query: ${err.message}. Response from server was: ${response}`);
} }
} }

View file

@ -660,14 +660,18 @@ export async function evaluateJsonQuery(data: any, jsonPath: string, jsonPathOpe
try { try {
response = JSON.parse(data); response = JSON.parse(data);
} catch { } catch {
response = typeof data === "number" || typeof data === "object" ? data : data.toString(); response = (typeof data === "object" || typeof data === "number") && !Buffer.isBuffer(data) ? data : data.toString();
} }
try { try {
// If a JSON path is provided, pre-evaluate the data using it. // If a JSON path is provided, pre-evaluate the data using it.
response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response; response = (jsonPath) ? await jsonata(jsonPath).evaluate(response) : response;
if (typeof response === "object" || Array.isArray(response) || response instanceof Date || typeof response === "function") { if (response === null || response === undefined) {
throw new Error("Empty or undefined response. Check query syntax and response structure");
}
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`); 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`);
} }
@ -700,7 +704,7 @@ export async function evaluateJsonQuery(data: any, jsonPath: string, jsonPathOpe
expected: expectedValue.toString() expected: expectedValue.toString()
}); });
if (response === undefined || status === undefined) { if (status === undefined) {
throw new Error("Query evaluation returned undefined. Check query syntax and the structure of the response data"); throw new Error("Query evaluation returned undefined. Check query syntax and the structure of the response data");
} }
@ -709,7 +713,8 @@ export async function evaluateJsonQuery(data: any, jsonPath: string, jsonPathOpe
response // The response from the server or result from initial json-query evaluation response // The response from the server or result from initial json-query evaluation
}; };
} catch (err: any) { } catch (err: any) {
response = (response.toString().length > 50) ? `${response.substring(0, 50)}… (truncated)` : response.toString();// Truncate long responses to the console response = JSON.stringify(response); // Ensure the response is treated as a string for the console
response = (response && response.length > 50) ? `${response.substring(0, 100)}… (truncated)` : response;// Truncate long responses to the console
throw new Error(`Error evaluating JSON query: ${err.message}. Response from server was: ${response}`); throw new Error(`Error evaluating JSON query: ${err.message}. Response from server was: ${response}`);
} }
} }