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

Lab 4: (Optional) Error Handling

Overview

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. Head to the post:\product flow.

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

  2. Insert the Try component component after the Set Variable component

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

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.

    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.

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.

  2. Configure with the following values

    NameValue
    TypeAPP:CONFLICT
    description#[error.description]

Step 4: Handle a new Error

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

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

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

  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}
    

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

  5. Click button. A new window will appear.

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

  7. Remove the existing content and simply write 409 in the script box.

    At the end you will have something similar to this:

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:

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

    TIP

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

Summary

In this module we have seen the following:

Go Further:

Congratulations! You have completed Lab 4.