- Overview
- Step 1: Add Try Scope
- Step 2: Catch the Error
- Step 3: Raise a new Error
- Step 4: Handle a new Error
- Step 5: Test the error
- 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.
- Go to the palette and search for the Try component. 
- Insert the Try component component after the Set Variable component 
- 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.
- 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. 
- 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.
- Drag and drop Raise Error component inside the On Error Propagate component.  
- Configure with the following values - Name - Value - Type - APP: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.
- Go to the Error Handling in the - api-mainflow and add a new On Error Propagate Handler
- Click inside and configure - APP:CONFLICTin Type. 
- Add a Transformation Component inside - We are going to return a payload and a httpStatus variable with the error code. 
- 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 
- Click  button. A new window will appear. button. A new window will appear.
- Select Variable from the drop down list and set - httpStatusas the Variable Name. 
- Remove the existing content and simply write - 409in the script box. - At the end you will have something similar to this:  
Step 5: Test the error
- Run the API, and validate you receive a - 409by calling- POSTon “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:
- See the link Try Scope for more information 
- See the link Raise Error Component for more 
- See the link Error Handling for more information 
Congratulations! You have completed Lab 4.