Why try/catch doesn't work with failed queries?

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?

1 Like
return FailedSimpleMySqlInsertQuery.trigger({
	onFailure: (error) => {
		// successful calles on error
		console.info('onError', error);

		// this does nothing!!
		throw new Error('throw error');
	},
})
.catch(err => {
	// this is not called!
	console.info('catch 1', err);
})
.then(
	(data) => {
		// this is called but should not! <<<<<<<<<
		console.info('ok', data);
		return 'hallo';
	},
	(err) => {
		// this is NOT called but should! <<<<<<<<<
		console.info('then error', err);
		return 'welt';
	}
)
.catch(err => {
	// this is not called!
	console.info('catch 2', err);
})
;

Hi @vangelov !

As we discussed via the support chat, this is likely related to a bug and I've linked this thread to that report! I'll follow up here once we have an update!

I have the same issue, and the same mitigation method

Hi @renatovico !

Thanks for raising this as well. We'll update this thread once this bug is resolved.

Confirming this hack does work around the bug. Looking forward to seeing a more general fix.

The bug seems to be still occurring. :frowning:

Same issue. Do you mind sharing what was in the support channel?

Can you share it with us please?

How can we workaround this:

result = asyncFailingFunction(bla);