Contact Us 1-800-596-4880

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:

  1. In the hello-world-app project in Studio, open hello-world-api.xml if it isn’t already open, and make sure you are viewing the Message Flow tab of the canvas.

  2. In Mule Palette, select Add Modules > Validation and drag it to the Mule Palette.

    View of Mule Palette with the Validation module selected

  3. 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.

    Validate size dropped into GET module

  4. Click Validate size to display the General configuration tab.

    Validate size configuration tab

  5. Set these values in the General tab:

    1. 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.

    2. Min: 6

    3. Max: 30

    4. Error options > Message: The specified message is either too long or too short.

  6. From the Studio main menu, select File > Save All to save your changes.

  7. Right-click in the canvas and select Run project hello-world-app.

  8. After you see the console message indicating hello-world-app is deployed, send a request using Advanced Rest Client or a similar tool:

    http://localhost:8081/api/greeting

    Your request client reports response headers containing 200 OK and Today the greeting is Hello.. This demonstrates the expected behavior when the greeting is within the limits you set in the Validate size operation.

  9. 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.

  1. In the hello-world-app project in Studio, open hello-world-api.xml.

  2. Click Set Payload, and then change the value to Hi..

  3. 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.

  4. Right-click Set Payload and select Add breakpoint.

  5. Repeat the previous step for Logger and Validate size.

  6. 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.

    Studio in Debugger view

  7. In Advanced REST Client, send a request to the /greeting endpoint:

    http://localhost:8081/api/greeting
  8. Return to Studio and notice that there’s a dashed line around Set Payload.

  9. In the canvas, click Set Payload to open the Set Payload node in the Mule Debugger.

  10. 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.

  11. Click the Next processor (curved yellow arrow) above the debugger window.

    Next processor control to move to next toggle point

    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.

  12. Click the curved yellow arrow again to move to the Validate size operation.

  13. 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.

    View of debugger displaying error for Validate size

  14. 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.

  15. Click the Next processor again to complete the flow. The Mule debugger pane is now empty because the process is complete.

  16. 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.

  17. Right-click on the canvas and select Stop project hello-world-app.

  18. 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.

  1. 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.

    Adding an error handler to GET module and configuring it

  2. 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.

  3. Drag a Set Payload operation into the new On Error Propagate error handler.

  4. Configure the new Set Payload operation:

    1. Double-click Set Payload to open the General tab.

    2. In Settings > Value:, replace payload with error.description.

  5. Click File > Save All to save your changes.

  6. Add breakpoints to Set Payload, Logger, and Validate size if you previously removed them.

  7. Right-click in the canvas and select Debug project hello-world-app.

  8. 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.

  9. 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.

    Debugger messages for Set Payload in error handler

  10. When you reach the APIkit Router in the message flow, you see that the error message replaces the original payload Hi..

    Breakpoint at APIkit Router showing error message in payload

  11. 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.

  12. Set the original payload back to Today the greeting is Hello..

  13. 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:

  1. From Package Explorer in Studio, right-click the hello-world-app project.

  2. Select Anypoint Platform > Publish to Exchange.

  3. 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.

  4. Accept the version number, which automatically increments for you.

  5. In Project type, select Example.

  6. Click Next.

  7. 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.

Developer Deep Dives

There’s a lot to learn about unit testing, error handling, and validation. Explore the details: