JS queries do not propagate errors from inner queries — here’s a fix

When using JavaScript queries in Retool, errors from inner queries (triggered inside the JS query) are not automatically propagated.

This pattern ensures the standard error propagation behavior, so your JS query fails as expected when an inner query fails:

let errorMessage;
const onFailure = errStr => {
  const error = JSON.parse(errStr);
  errorMessage = error.errorData;
};
const ensureExitWithError = () => {
  if (errorMessage) 
    throw new Error(errorMessage);
};

const result1 = await u_other_query.trigger({
  additionalScope: { someParam: 'someValue' },
  onFailure, // <-- IMPORTANT
});
ensureExitWithError(); // <-- IMPORTANT

// use result1 in your code

const result2 = await u_another_query.trigger({
  onFailure, // <-- !IMPORTANT
});
ensureExitWithError(); // <-- IMPORTANT

// use result2 in your code

return result1 + result2; // example of return

This ensures that any failed inner query will cause the main JS query to fail too, maintaining the expected error handling flow.

1 Like

Thanks for sharing, @saldmy! I'm going to move this over to the Community Show & Tell category. :+1: