return early instead of nesting if/else blocks

This commit is contained in:
dongfang committed 2025-02-12 02:53:09 +00:00
1 parent 9fe2de8969
commit 4d12608a22
1 file changed
+46 -45
+46 -45
View File
@@ -151,54 +151,55 @@ class NetworkService {
httpResponse.code = response.status;
if (expectedValue) {
// validate if response data match expected value
let result = response?.data;
this.logger.info({
service: this.SERVICE_NAME,
method: "requestHttp",
message: `Job: [${name}](${_id}) match result with expected value`,
details: { expectedValue, result, jsonPath, matchMethod }
});
if (jsonPath) {
const contentType = response.headers['content-type'];
if (contentType && contentType.includes('application/json')) {
try {
result = jmespath.search(result, jsonPath);
} catch (error) {
httpResponse.status = false;
httpResponse.message = "JSONPath Search Error";
return httpResponse;
}
} else {
httpResponse.status = false;
httpResponse.message = "Response Not JSON";
return httpResponse;
}
}
if (result === null || result === undefined) {
httpResponse.status = false;
httpResponse.message = "Empty Result";
return httpResponse;
}
let match;
result = typeof result === "object" ? JSON.stringify(result) : result.toString();
if (matchMethod === "include") match = result.includes(expectedValue);
else if (matchMethod === "regex") match = new RegExp(expectedValue).test(result);
else match = result === expectedValue;
httpResponse.status = match;
httpResponse.message = match ? "Match" : "Not Match";
if (!expectedValue) {
// not configure expected value, return
httpResponse.status = true;
httpResponse.message = this.http.STATUS_CODES[response.status];
return httpResponse;
}
httpResponse.status = true;
httpResponse.message = this.http.STATUS_CODES[response.status];
// validate if response data match expected value
let result = response?.data;
this.logger.info({
service: this.SERVICE_NAME,
method: "requestHttp",
message: `Job: [${name}](${_id}) match result with expected value`,
details: { expectedValue, result, jsonPath, matchMethod }
});
if (jsonPath) {
const contentType = response.headers['content-type'];
if (contentType && contentType.includes('application/json')) {
try {
result = jmespath.search(result, jsonPath);
} catch (error) {
httpResponse.status = false;
httpResponse.message = "JSONPath Search Error";
return httpResponse;
}
} else {
httpResponse.status = false;
httpResponse.message = "Response Not JSON";
return httpResponse;
}
}
if (result === null || result === undefined) {
httpResponse.status = false;
httpResponse.message = "Empty Result";
return httpResponse;
}
let match;
result = typeof result === "object" ? JSON.stringify(result) : result.toString();
if (matchMethod === "include") match = result.includes(expectedValue);
else if (matchMethod === "regex") match = new RegExp(expectedValue).test(result);
else match = result === expectedValue;
httpResponse.status = match;
httpResponse.message = match ? "Match" : "Not Match";
return httpResponse;
} catch (error) {
error.service = this.SERVICE_NAME;