http://localhost:8081/api/greeting
Step 4. Add Validation and Error Handling
So far, you’ve created an API and tested the HTTP request with a third-party client, and then published the API to Exchange. Now you’ll use Studio to add validation and a unit test. In Studio, you’ll use the scaffolding that’s already created to:
-
Validate input using the Validation module.
-
Create an error handler for one error condition.
Step 4.1: Validation
Add an operation from the Validation module to the API implementation to ensure that user input is appropriate for the API. Your API is very simple, so your testing is also simple. You’ll test whether the size of the payload is within an acceptable range.
Add the Validate size operation to the hello-world-app
project:
-
In the
hello-world-app
project in Studio, openhello-world-api.xml
if it isn’t already open, and make sure you are viewing the Message Flow tab of the canvas. -
In Mule Palette, select Add Modules > Validation and drag it to the Mule Palette.
-
From the list of operations, drag Validate size to the Studio canvas and drop it inside the endpoint module to the right of Set Payload.
-
Click Validate size to display the General configuration tab.
-
Set these values in the General tab:
-
Value: Click the fx button to change from literal mode to expression mode, and then enter
payload
after the characters in the text box. Notice the closing bracket is supplied for you. -
Min:
6
-
Max:
30
-
Error options > Message:
The specified message is either too long or too short.
-
-
From the Studio main menu, select File > Save All to save your changes.
-
Right-click in the canvas and select Run project hello-world-app.
-
After you see the console message indicating
hello-world-app
is deployed, send a request using Advanced Rest Client or a similar tool:Your request client reports response headers containing
200 OK
andToday the greeting is Hello.
. This demonstrates the expected behavior when the greeting is within the limits you set in the Validate size operation. -
Right-click the canvas and select Stop project hello-world-app.
To test that the validation works when the payload is too short, use the debugger to step through each event before the error is triggered.
-
In the
hello-world-app
project in Studio, openhello-world-api.xml
. -
Click Set Payload, and then change the value to
Hi.
. -
In Mule Palette, select Favorites > Logger, and then drag the logger between Set Payload and Validate size. The logger stops the process to help debugging. No configuration of the logger is needed.
-
Right-click Set Payload and select Add breakpoint.
-
Repeat the previous step for Logger and Validate size.
-
Right-click the canvas and select Debug project hello-world-app. If prompted to open the Mule Debug perspective, select the Remember my decision checkbox and click Yes.
-
In Advanced REST Client, send a request to the
/greeting
endpoint:http://localhost:8081/api/greeting
-
Return to Studio and notice that there’s a dashed line around Set Payload.
-
In the canvas, click Set Payload to open the Set Payload node in the Mule Debugger.
-
Open Set Payload and see that the payload value is
Hi.
, which is three characters.Since you configured the Validate size operation to have a minimum of 6 characters, this will trigger an error.
-
Click the Next processor (curved yellow arrow) above the debugger window.
If you hover over it to ensure you have the correct control, the label Next processor (F6) appears. The debugger pane now shows the Logger connector.
-
Click the curved yellow arrow again to move to the Validate size operation.
-
Click the curved yellow arrow again, and notice that an error message you defined is now in the error object in the debugger pane, and a dotted-line shows in the Validate size operation.
-
Click the Next processor again, and notice that the canvas now displays the beginning of your message flow, with a red dotted-line around the APIkit Router, one of the scaffolding items added during import. The router handles the error.
-
Click the Next processor again to complete the flow. The Mule debugger pane is now empty because the process is complete.
-
Look at Advanced Rest Client. You should see
500 Server Error
and the payload value.To see the error you defined returned instead of the payload, you’ll need to configure one of the error handlers.
-
Right-click on the canvas and select Stop project hello-world-app.
-
Select Window > Perspective > Open Perspective > Mule Design to exit the debugger view.
Step 4.2: Add Error Handling
Now you’ll add an error handler for the validation you’ve set up.
-
In Studio Mule Palette, select Core > On Error Propagate, and then drag the error handler on to Error handling in get:\greeting:hello-world-app-config.
-
In the General configuration tab, configure the error handler:
-
Display Name:
On Error Propagate
-
Type: Click the search icon and select VALIDATION:INVALID_SIZE.
-
Don’t change the other default values.
-
-
Drag a Set Payload operation into the new On Error Propagate error handler.
-
Configure the new Set Payload operation:
-
Double-click Set Payload to open the General tab.
-
In Settings > Value:, replace
payload
witherror.description
.
-
-
Click File > Save All to save your changes.
-
Add breakpoints to Set Payload, Logger, and Validate size if you previously removed them.
-
Right-click in the canvas and select Debug project hello-world-app.
-
After the app is deployed, send the query
http://localhost:8081/api/greeting
from Advanced Rest Client, and step through each breakpoint using the curved yellow arrow. -
When you reach Set Payload in On Error Propagate, you can see that the payload is set to the error message you created in Validate size.
-
When you reach the APIkit Router in the message flow, you see that the error message replaces the original payload
Hi.
. -
After you step through and look at the response in Advanced Rest Client, you see that a 500 Server Error is returned along with the error message.
-
Set the original payload back to
Today the greeting is Hello.
. -
Set the view in Studio to Window > Perspective > Open Perspective > Mule Design.
Next, you’ll publish the revised app.
Step 4.3: Publish Your Revised App
Make your improved API available on Exchange:
-
From Package Explorer in Studio, right-click the
hello-world-app
project. -
Select Anypoint Platform > Publish to Exchange.
-
Be sure to select the same Business Group as you did before. The Next button won’t be active until you select a valid business group.
-
Accept the version number, which automatically increments for you.
-
In Project type, select Example.
-
Click Next.
-
Click Finish.
Studio shows you the direct link to your newly published API version. You can copy it to share with others.
What’s Next
Now you can deploy your API so that anyone can send a request to the /greeting
endpoint.