Queries in Retool return a promise so this code works correctly:
const result = await someQuery.trigger()
In all modern browsers that support async/await
, you can also use try/catch
. However, for some reason, Retool doesn't call the catch
block when a query fails.
For example, let's say we have two JS queries:
failingQuery:
throw new Error('error')
and
callingQuery:
try {
const result = await failingQuery.trigger()
} catch(error) {
console.log('error:', error)
}
The code in the catch
block above is never called.
Using onFailure
handler of the query does work:
const result = await failingQuery.trigger({
onFailure: (error) => {
console.log('error:', error)
}
})
By the way this doesn't seem like some kind of technical limitation as we easily correct it by using this utility function:
function triggerQuery(query, additionalScope) {
return new Promise((resolve, reject) => {
query.trigger({
additionalScope,
onSuccess: resolve,
onFailure: reject
})
})
}
Now we can write the initial query as so:
callingQuery:
try {
const result = await triggerQuery(failingQuery)
} catch(error) {
console.log('error:', error)
}
Handling errors with try/catch
makes for a more readable code and is supported in all modern browsers. Why doesn't it work in Retool?