Best Practices for Handling Errors

When triggering resource queries from js queries what techniques are you using to handle errors? Is it best practice to let the called query or the calling query handle the errors?

When triggering a Retool DB query and handling the error within the calling js, I'm having an issue collecting dbResp.error. It's null on the first run after a refresh of an app. "Keep variable references inside the query in sync with your app" didn't seem to help.

const dbResp = await updateDb.trigger({additionalScope:{id:0, data:{email:''}}});

if (!dbResp) {
  // dbResp is undefined
  throw new Error(updateDb.error) // null on first run after app refresh

// do other stuff

return {status:-1, resp: dbResp}

I've tried something like this as well.

if (!dbResp) {
  return {status:1, error:'failed to run updateDb'}

then use failure conditions. [EDIT] To clarify, this does work, updateDb.error exists. I'm just not sure I care for this pattern much.
Screenshot 2023-10-27 at 10.11.28 AM

When triggering an REST API resource and handling the error within the calling js query, I can retrieve apiResp.error

const apiResp = await updateViaApi.trigger({additionalScope:{data:1}});

if (!apiResp) {
  throw new Error(updateViaApi.error) // error string exists

console.log('do other stuff...')
return {status:-1, data: apiResp}

I'm also curious why failed responses are undefined. For example, when running a failed Retool DB query manually the response is

How do you handle your errors?

1 Like

Hi @matth

If you're ok with the Javascript query triggering the Retooldb query being "successful," one option could be:

    let err;
    const dbResp = await updateDb.trigger({additionalScope:
    {id:0, data:{email:''}}, onFailure: function (data){

    return {status:-1, resp: err}