Code-based Actions: Capture API errors

Code actions allow you to make API requests. Sometimes these requests fail and you want to get more info to see the reason that caused the error.

API errors can be caught within code actions using a simple try catch block. Within the catch the error object holds any request information, including an optional response sent by the server.

The following code demonstrates how you can handle different error states.

async payload => {

  try {  
    // Make a request  
    const result = await request.get('MY API ENDPOINT')  
    // ...

  } catch(err) {  
    if (err.response) {  
      // The request was made and the server responded with a status code  
      // that falls out of the range of 2xx  
      console.error('Failed with response', err.response.data)  
    } else if (err.request) {  
      // The request was made but no response was received  
      console.error('Failed request', err)  
    } else {  
      // Something happened in setting up the request that triggered an Error  
      console.error('Failed in general', err)  
    }  
  }  
}