Contact Us 1-800-664-9073

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:

  1. In the hello-world project in Studio, open hello-world.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 drag and drop area in the left side of Mule Palette.

    View of Mule Palette with Validation selected

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

    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.

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

    http://localhost:8081/api/greeting

    Your request client should report response headers containing 200 OK and Today the greeting is Hello.. This demonstrates the expected behavior when the greeting is within the limits we just set in the Validate size connector.

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

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

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

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

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

  5. Right-click Logger and select Add breakpoint.

  6. Right-click Validate size and select Add breakpoint.

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

    Studio in Debugger view

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

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

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

  11. Click the curved yellow arrow above the debugger window.

    Control to move to next toggle point

    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.

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

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

    View of debugger displaying error for Validate size

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

  15. Click the curved yellow arrow again to complete the flow. Notice that the Mule debugger pane is now empty because the process has completed.

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

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

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

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

    Adding an error handler to GET module and configuring it

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

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

  4. Configure the new Set Payload connector:

    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. Righ-click in the canvas and select Debug project hello-world.

  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 payload set to the error message we created in Validate size is now the payload.

    Debugger messages for Set Payload in error handler

  10. When you reach APIkit Router at the top of the message flow, you can see the error message replaces the original payload Hi..

    Breakpoint at APIkit Router showing error message in payload

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

  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, we’ll publish our revised API.

Step 4.3: Publish Your Revised API

Make your improved API available on Exchange:

  1. From the Package Explorer in Studio, right-click the hello-world 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

Let’s deploy our 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:

View on GitHub