Exception Handling in Oracle Visual Builder
This article demonstrates how to perform Exception Handling for REST action in Oracle Visual Builder.
If you want to perform exception handling for REST actions you can achieve it by surrounding any REST action with Try-Catch blocks where the actual REST API call runs under the Try block. If the API returns any error response, that error will be handled in the Catch block. However, the catch block will only catch exceptions that are thrown during the execution of the try block. If the REST API returns an error response like a JSON object with an error message, it might not throw an exception that would be caught. So to handle this type of exception, we need to throw an exception explicitly from the try block.
We will use a Business Object with id and name as the fields and use its GET REST endpoint to perform exception handling on it.
The following are the steps:
- Create a VB application and a web application under the VB application using the Web application navigator.
- Create 3 variables with the following properties:
- A variable Utensils of type UtensilsType with the following items:
- id (Number)
- name (String)
- A string variable Id
- A string variable ErrorStatus
- A variable Utensils of type UtensilsType with the following items:
- Go to the main start page, drag and drop the Input Text component from the Components pallet to the design area, as shown in the image below. Bind the Input text with Id variable of the page.
- Then drag and drop the oj-bind-text component from the Components pallet to the design area just to the right side of the Input Text and bind the Text component with the name attribute of the Utensils variable, as shown in the image below.
- Then drag and drop the Button component from the Components pallet to the design area below Input Text, as shown in the image below. Give Search as a label property to the button
- Now go to Events in the Properties panel and click on + Event Listener to create an action chain to the Search button that will trigger whenever you provide some Id in the Input Text field and click on Search. This will create the ButtonActionChain action chain and open the action chain canvas.
- Now on the action chain canvas, go to the Actions pallet, drag and drop the Call REST action to the canvas. In the Properties panel, click on Endpoint Select An Endpoint selection canvas will open. Select Business Objects -> Utensils -> GET (One) REST endpoint from the list.
- Now in the Properties panel under Input Parameters, click on A mapper window will open. Map the Id variable from the left side to the Utensils_Id API input variable. Click on Save. This will add GET (One) REST API in the Call REST action and its response will be saved into the Call REST input response variable.
- Now right-click on the Call REST action and then select Surround with Try-Catch.
- Then drag and drop the If action from the Actions pallet under the Call REST Then click on the Code button under the Properties panel. This will open the JS code console where you can write direct JS code.
- Add: if(!response.ok) condition to the JS console. This if block will check if API is giving ok as false means API is giving some error based on the response status. Now we will handle this error explicitly under this if block.
- Now under the if block, add the below-formatted code.
if (!response.ok) { // Create an error object from the response const error = new Error('API Error'); $variables.ErrorStatus = "Error Occured while fetching data. Response status: " + response.status; throw error; }
In the above code, firstly we are explicitly creating an error object and throwing this error object at the end of the if block. This error object will be implicitly caught by the Catch block. In between we will assign the error message with the error status code to the ErrorStatus variable and this variable will be used to show the error in the Fire Notification action (Note: You can add your custom error logic in between which you want to be caught in the Catch block).
- Now drag and drop the Assign Variable action from the Actions pallet under the Create Branch section of the if block. This will create the else block below the if block and the code inside this else block will execute if the API responds correctly.
- Select the Utensils variable from the dropdown of the Variable LOV field in the Properties section and bind its value to response.body.
- Now inside the Catch block, drag and drop the Fire Notification action from the Actions pallet, and under the Summary field in the Properties panel, select the ErrorStatus variable. Whenever the REST API returns an error as a response, a notification will be fired showing the error message stored inside the ErrorStatus variable.
Now If you enter the correct Utensil Id inside the Enter Utensil Id input field and click on Search, the name of the corresponding Utensil will be shown on the screen. But If you enter the wrong or invalid Utensil Id, the error message will be shown in the Fire Notification action with the Error Status code in it.
If you liked the article, please like, comment, and share.
Please look at my YouTube channel for Oracle Integration-related videos and don’t forget to subscribe to our channel to get regular updates.
Further Readings
Restrict File Size in Oracle Visual Builder
Dynamic filter on table in Oracle Visual Builder
Integrating Oracle Process Automation in Oracle Visual Builder