1. Overview
  2. Step 1: Add Try Scope
  3. Step 2: Catch the Error
  4. Step 3: Log Salesforce Response
  5. Step 4: Raise a new Error
  6. Step 5: Handle a new Error
  7. Summary

Lab 3: Run the Mule app from Anypoint Studio

Overview

In Lab 2, you built the flow that will create a new user. If there’s an error in Salesforce, you’ll receive a Bad Request. This error message doesn’t give you full visibility into what really happened. In this lab you are going to learn how to Handle errors.

Step 1: Add Try Scope

The Try scope enables you to handle errors that may occur when attempting to execute any of the components inside the Try scope.

  1. Go to the palette and search for the Try component.

  2. Insert the Try component after the Salesforce connector.

  3. Move the Validation icon inside the Try component.

Step 2: Catch the Error

When you handle the error you have two options.

  • On Error Continue: This will catch the error and continue with the flow execution.

  • On Error Propagate: This option will catch the error, but will throw it up.

You could eventually use one or the other depending on the error type.

In this case, we are going to propagate the error.

To do that, we are going to add an On Error Propagate handler to the Error handling part.

  1. Go to the palette and search for On Error Propagate. Drag and Drop the component inside the Error handling part.

  2. Click on the component.

  3. In the Type section click the magnifier glass on the right

  4. Select VALIDATION:INVALID_BOOLEAN

Step 3: Log Salesforce Response

In this step, the idea is to log the Salesforce Response.

  1. Add a Logger component to log the response.

  2. Configure the component with the value output application/json --- payload

    When you press the f(x) button, you will see that a #[] is added to the field. This is because we are going to use a dataweave expression to log the Salesforce response.

Step 4: Raise a new Error

After logging the response, we are going to raise a new error

  1. Search for Raise Error component and insert after the logger.

  2. Configure the component with the following values:

    • Type: APP:CONFLICT

    • Description: #[error.description]

      In the last step, we are raising a new error type called APP:CONFLICT and we are populating the details, with the Salesforce error message.

Step 5: Handle a new Error

Now we are going to build the Response for this exception.

  1. Go to the APIKit Error handling. Add an On Error Propagate component at the bottom.

  2. Click on the Component and configure the Type value with APP:CONFLICT.

  3. Add a Transform component inside the On Error Propagate.

  4. Add the following transformation

         %dw 2.0
         output application/json
         ---
         {message: error.description}
    

    We are mapping the description message we set in the Raised Error component. If you want to see the complete error object, you can add a logger before the transformation.

    We also need to set the http status code. To do that we are going to add a variable inside the transformation component

  5. Click on the inside the transformation component.

    A new window will appear

  6. Select Variable in the combo and write httpStatus as Variable Name.

  7. Click OK.

  8. Remove the script and only add 409.

    Save all the changes then run the project again and try to create a user twice. You should receive 409 as a response.

Summary

In this lab, we have achieved the following:

  • Add Try Scope

  • Catch the error

  • Log Salesforce response

  • Raise a new Error

  • Handle a new Error

Go Further:

Congratulations! You have completed Lab 4.