Business Continuity Planning: JavaScript Error Handling Methods

 

Overview

Studio Functions offers self-service function management, allowing you to map a task based on custom, preconfigured JavaScript code that executes a specific action in your Studio flow. JavaScript error handling can help you use this feature safely and reduce the impact of errors in your custom code. If you are new to JavaScript or wish to learn more about this topic, there are several open resources available, including the W3 Schools and MDN, that provide in-depth guides for writing your functions.

The try {} and catch {} methods provide an easy solution to JavaScript error handling. The try {} block first attempts to execute the block of code that you define. If an error occurs, the catch {} block is executed instead. This method can be beneficial if you have large, complex functions that could be more difficult to troubleshoot or more prone to errors. 

function (args) {
  // Your function inputs will be listed here
  const { my_input } = args;
  // ⬇️ Write your code below ⬇️
  try {
    //Block of code to try
  } catch (error) {
    //Block of code to execute if an error occurs
  } finally {
    //Optional block of code to execute regardless of errors
  }
}

Best Practices 

  • Return the message property of JavaScript errors in an output variable to create detailed error messages. You can then expose this variable as context in Conversations to provide valuable information for your agents.
  • If your Functions generate output values used for dynamic, context-based routing, use the catch{} statement to provide a fallback value. This will ensure that interactions can be correctly routed regardless of error. 

 

Additional JavaScript Error Handling Examples 

Return Fallback Ring Group Assignment 

This function parses a JSON object, accesses the ring_group property, and returns this information in an output variable of the same name. This type of function may be useful for parsing a customer’s record from an external system (such as a CRM) and assigning their call directly to the correct ring group.

Without error handling, if the input array was blank or undefined, an execution error would occur and the ring_group output variable would remain blank. Downstream Studio steps that reference this variable may also fail. The try and catch blocks here will instead catch these errors and provide “general-support” as a fallback value to ensure all calls are successfully routed to an agent. 

function (args) {
  // Your function inputs will be listed here
  const { json_data } = args;
  // ⬇️ Write your code below ⬇️
  let ring_group;
  try {
    ring_group = JSON.parse(json_data).ring_group;
  } catch (error) {
    ring_group = "general-support"
  } finally {
    return {ring_group}
  }
}

 

Expose Error Message in Agent Context 

This general-purpose example can be used with nearly any code contained in the try block. By accessing the message property (error message) of the error object, we can provide a more detailed, actionable error message.

function (args) {
  // Your function inputs will be listed here
  const { your_input_variable } = args;
  // ⬇️ Write your code below ⬇️
  try {
    //Block of code to try. You can insert any code here.
  } catch (error) {
    your_output_variable = "A function error occurred. Message: " + error.message + ". Contact your Talkdesk Admin."
  } finally {
    return {your_output_variable}
  }
}

When exposed as context in Conversations, errors in your Function can return a message like the one below. This can empower your agents to provide more specific information to Administrators and reduce troubleshooting time.

All Articles ""
Please sign in to submit a request.