http://localhost:8081/api/greeting
Step 4. Add Validation and Error Handling
So far, we’ve created an API and tested the HTTP request with a third-party client, and then published the API to Exchange. Now we’ll use Studio to add validation and a unit test. In Studio, we’ll use the scaffolding 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. Our API is very simple, so our testing will be simple. We’ll test that the size of the payload is within an acceptable range.
Add the Validate size operation to the hello-world
project:
-
In the
hello-world
project in Studio, openhello-world.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 drag and drop area in the left side of Mule Palette.
-
From the list of operations displayed, drag Validate size to the canvas and drop it to the right of Set Payload, inside the endpoint module.
-
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.
-
After you see the console message indicating
hello-world
has deployed, send a request using Advanced Rest Client or similar:Your request client should report response headers containing
200 OK
andToday the greeting is Hello.
. This demonstrates the expected behavior when the greeting is within the limits we just set in the Validate size connector. -
Right-click the canvas and select Stop project hello-world.
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
project in Studio, openhello-world.xml
. -
Click Set Payload, and then change the value to
Hi.
. -
In Mule Palette, select Favorites > Logger, and then drag and drop the logger in between Set Payload and Validate size. The logger will help us stop the process to help debugging. No configuration of the logger is needed.
-
Right-click Set Payload and select Add breakpoint.
-
Right-click Logger and select Add breakpoint.
-
Right-click Validate size and select Add breakpoint.
-
Right-click the canvas and select Debug project hello-world. If you are asked 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 above the canvas. Open Set Payload and see that the payload value is
Hi.
, three characters. -
Click the curved yellow arrow above the debugger window.
If you hover over it to ensure you have the right 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 connector.
-
Click the curved yellow arrow again, and notice that that an error message you defined is now in the error object in the debugger pane, and a red dotted-line shows in the Validate size connector.
-
Click the curved yellow arrow again, and notice that the canvas now displays the top 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 curved yellow arrow again to complete the flow. Notice that the Mule debugger pane is now empty because the process has completed.
-
Look at Advanced Rest Client. You should see
500 Server Error
and the payload value.To see the error we defined returned instead of the payload, we’ll need to configure one of the error handlers.
-
Right-click on the canvas and select Stop project hello-world.
-
Select Window > Perspective > Open Perspective > Mule Design to exit the debugger view.
Step 4.2: Error Handling
Now we’ll add an error handler for the validation we’ve set up.
-
In Studio Mule Palette, select Core > On Error Propagate, and then drag and drop the error handler on Error handling in get:\greeting:hello-world-config.
-
In the General tab below the canvas, configure the error handler:
-
Display Name:
On Error Propagate
-
Settings Type: Click the search icon and select VALIDATION:INVALID_SIZE from the drop-down menu.
-
Don’t change the other default values.
-
-
Drag a Set Payload connector into the new On Error Propagate error handler.
-
Configure the new Set Payload connector:
-
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.
-
Righ-click in the canvas and select Debug project hello-world.
-
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 payload set to the error message we created in Validate size is now the payload.
-
When you reach APIkit Router at the top of the message flow, you can see the error message replaces the original payload
Hi.
. -
When you have stepped all the way through and look at the response in Advanced Rest Client, you’ll 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, we’ll publish our revised API.
Step 4.3: Publish Your Revised API
Make your improved API available on Exchange:
-
From the Package Explorer in Studio, right-click the
hello-world
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.