05 Apr 2024 06:43 PM - edited 05 Apr 2024 06:51 PM
Hello Dynatrace family,
I have been having difficulty understanding how to interact with workflow tasks behavior and how to be able to control those behaviors.
I have a javascript code that checks if a variable that I captured from previous task matches it or not, if it doesn't match I want my task to fail but It always ends up being successful even when the value doesn't match with what I defined.
// optional import of sdk modules
import { execution } from '@dynatrace-sdk/automation-utils';
import { executionsClient } from '@dynatrace-sdk/client-automation';
export default async function ({ execution_id }) {
const ex = await execution(execution_id);
var results:JSON = await executionsClient.getTaskExecutionResult({ executionId: execution_id, id: "execute_dql_query_1" });
var driftResults = results.records;
let errorIdValue = driftResults[0].errorid;
function checkIfSpecificerrorid(value) {
const specificNumber = 6000;
const numericValue = Number(value);
if (numericValue !== specificNumber){
throw new Error("Value does not match " + specificNumber);
}
console.log("value matches the specific number: " + value);
}
try {
if (errorIdValue !== undefined) {
checkIfSpecificerrorid(errorIdValue);
}
} catch (error) {
console.error(error.message);
}
}
How can I make this code to fail the task rather than successfully complete?
Edit: the content of driftResults is :
[
{
"errorid" : "6001"
}
]
Solved! Go to Solution.
16 Apr 2024 10:33 AM - edited 16 Apr 2024 10:35 AM
Hi @shahin24093 ,
you can throw the error in your catch block to make this work.
By having a catch block, you are telling JavaScript "hey, if there is an error thrown in checkIfSpecificerrorid let me (the person who is writing the code) handle it, don't fail the action".
By re-throwing the error the action fails when checkIfSpecificerrorid throws. See here:
// optional import of sdk modules
import { execution } from '@dynatrace-sdk/automation-utils';
import { executionsClient } from '@dynatrace-sdk/client-automation';
export default async function ({ execution_id }) {
const ex = await execution(execution_id);
var results: JSON = await executionsClient.getTaskExecutionResult({ executionId: execution_id, id: "execute_dql_query_1" });
var driftResults = results.records;
let errorIdValue = driftResults[0].errorid;
function checkIfSpecificerrorid(value) {
const specificNumber = 6000;
const numericValue = Number(value);
if (numericValue !== specificNumber) {
throw new Error("Value does not match " + specificNumber);
}
console.log("value matches the specific number: " + value);
}
try {
if (errorIdValue !== undefined) {
checkIfSpecificerrorid(errorIdValue);
}
} catch (error) {
console.error(error.message);
throw error
}
}
Note the "throw error" line after the "console.error(error)" line. With this you will get the action to fail. The output will be the following: