Best Practice for capturing error in top query

  • Goal: Find the best structure for having a main query on a submit perform multiple sub-queries either other queries or from resrouces and if any fail make the top main query fail

  • Details: After some testing, the best i have come up with is this code

// Initialize an error tracking object
let query_errors = {
    get_address_types2: null,
    get_address_types3: null
    // Add more queries here as needed
};


// 1st Query
await get_address_types2.trigger({
    onFailure: (data) => { query_errors.get_address_types2 = data; }
});
if (query_errors.get_address_types2) {
    throw new Error(`Error in get_address_types2: ${query_errors.get_address_types2}`);
}

// 2nd Query
await get_address_types3.trigger({
    onFailure: (data) => { query_errors.get_address_types3 = data; }
});
if (query_errors.get_address_types2) {
    throw new Error(`Error in get_address_types2: ${query_errors.get_address_types2}`);
}


// Check for any errors
for (const [query, error] of Object.entries(query_errors)) {
    if (error) {
        console.log(error)
        throw new Error(`Error in ${query}: ${error}`); // Use if you don't check for errors in-line. But if you do not use the inline checks, use this only the log
    }
}

// If all queries succeeded
return "all good";

I tried throwing an error on the onFailure but was having issues with the top query still running successfully.

i also messed with

let failure_msg = null

await get_address_types2.trigger({
    onFailure: (data) => { failure_msg = data; }
});

if (!failure_msg) {
    return "all good";
} else {
    throw new Error(failure_msg);
}

Is there a better way that someone is using to ensure that all internally called functions are successful. Most of queries i am doing Synchronous with awaits. I just wanted to see what the best practices within retool is and what people have come up with

Hey @PadenM,


We're discussing a similar issue here :blush:

1 Like