Lab 4: (Optional) Error Handling


In Lab 2, you implemented the API. Maybe it happened you’ve got a 500 error trying to creating a product with a repeated Unique ID. The error message does not describe any particular error. 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.

In this case, the Product Number in a product, is unique. So when we do an insert we could get an error. We are going to throw a 409 status code with the details of the error.

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

  2. Insert the Try component component at the beginning of the post flow

  3. Move the insert component inside. You should see something similar to this

    module8 lab2 try flow

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.

    module8 lab2 propagate

    We only want to catch the Query Execution error, so that’s what we are going to configure.

  2. Click on the Error Propagate component and in Type write DB:QUERY_EXECUTION. You can also select from the drop down list.

    module8 lab2 propagate config

Step 3: Raise a new Error

In this step, we are going to raise a new exception with the DB:QUERY_EXECUTION message.

  1. Drag and drop Raise Error component inside the On Error Propagate component.

    module8 lab2 raise error
  2. Configure with the following values

    Name Value





    module8 lab2 raise error config

Step 4: Handle a new Error

Now we need to go to the API Kit Error Handling and add a new On Error Propagate Handler to return 409.

  1. Go to the Error Handling in the product-api-main flow and add a new On Error Propagate Handler

  2. Click inside and configure APP:CONFLICT in Type.

    module8 lab2 api kit propagate
  3. Add a Transformation Component inside

    We are going to return a payload and a httpStatus variable with the error code.

  4. In the payload configuration we are going to return the error description, so copy and paste the following script:

    %dw 2.0
    output application/json
    {message: error.description}
    module8 lab2 error desc transform

    We also are going to return a http status code. To do that we need to create a variable

  5. Click module8_lab2_add_target button. A new window will appear.

  6. Select Variable from the drop down list and set httpStatus as the Variable Name.

    module8 lab2 add variable menu
  7. Write 409 in the script box.

    module8 lab2 add variable output

    At the end you will have something similar to this:

    module8 lab2 api kit conflict handlerpng

Step 5: Test the error

  1. Run the API, and validate you receive a 409 by calling POST on "products" with an existing product number.

    You should see something like this:

    module8 lab2 409 console

Try to return a 409 in the update flow, when a product doesn’t exist.

Enclose the Update Product component with a Try Scope and Raise an Exception as we did before.


In this module we:

Go Further: