This article explains the basics of error handling in APIs in API Connect 2018.

Your API

  • Build and test your API.

  • In this example the API (v1.0.0)is as follows: a MAP policy to read in three input query parameters (an account_id and a 2 target URLs), two INVOKE policies (to call the target URLs) and a GATEWAYSCRIPT policy (to format the response from the target URLs).

Case 1: Stop-on-Error (OperationError)

  • The requirement here is to add a basic error handling function around the INVOKE policies. This example will show how to use the Stop-on-error feature of the INVOKE policy.

  • The first step is to activate the Catch functionality for the API: ensure the “Show catches” button is toggled to be on.

  • Click on “catch” to enter the Catches of the API.

  • The Catches can now be updated.

  • Click on “+Catch” to add a catch to the API.

  • Click on “search errors”, then scroll through the drop-down list of errors. Select OperationError.

  • Drag a GATEWAYSCRIPT policy to the catch, detailing the messages to be sent on error.

  • Errors can be thrown in 2 ways. Either via the Stop-on-error function in an Invoke using a valid error, or using the THROW policy. In this section the focus is on Stop-on-error.

  • In the first INVOKE policy set the Stop-on-error function to be used with the OperationError already selected in the Catch created. Click on the first INVOKE policy.

  • Scroll down the INVOKE policy. Click on Stop-on-error.

  • Select OperationError from “search errors…” and Save the API.

  • Now re-publish.

  • Test the error API using working query inputs.

  • A valid response is returned.

  • Now test the error condition by inputting an invalid URL.

  • The error condition is triggered.

Case 2: THROW policy (custom error condition)

  • The requirement here is still to add a basic error handling function around the INVOKE policies. This example will show how to use the THROW policy with a custom error condition to send an error without using the Stop-on-error feature.

  • First save v1.0.0 of the API as v2.0.0.

  • Add a new case to the Catch and create a custom error called “Invoke2Error” as the error condition.

  • Drag a GATEWAYSCRIPT policy to the catch, detailing the messages to be sent on error.

  • For an error to be caught by the catch an error must first be thrown, so add a THROW policy to the Assembly after the INVOKE. Use a SWITCH Policy to check for a non-200 code return from the INVOKE.

  • Edit the SWITCH cases. Click on “switch”, and set a condition for the SWITCH by clicking on “edit condition”.

  • Set the condition to follow the SWITCH policy if the INVOKE does not return a 200, otherwise it will ignore the SWITCH policy.

  • Edit the THROW policy to throw the Invoke2Error.

  • Note that by creating the custom error in the THROW policy the Invoke2Error condition will appear in the dropdown for the Catch cases.

{width=”3.45370406824147in” height=”3.700396981627297in”}

  • Finally set Stop-on-error in the second INVOKE policy but do not throw an error to Invoke2Error. Save the API.

  • Now re-publish.

  • Test the error API using working query inputs.

  • A valid response is returned.

  • Now test the error condition by inputting an invalid URL.

  • The error condition is triggered.