1. Home
  2. Tutorials
  3. Working with APIs
  4. Respond to API Errors

Respond to API Errors

When your app deals with external data via an API, there’s always a possibility that something will go wrong. By returning error codes from your API requests, you can build processing into your app to handle any problems that occur during runtime. Each route in your Swagger/OpenAPI spec will indicate one or more status codes – typically these include a variety of codes representing a successful request, such as 200 for GET endpoints, or 201 for POST endpoints. Depending on the processing your API carries out, you may use status codes beginning with 4xx to indicate errors, such as 401 for an unauthorized response. By handling these in your Dropsource project, you can take steps to cope with any unsuccessful requests that crop up.

With an API containing error status codes imported into your project and a request added to a page in the app:

  1. If you want to display information about the error in the page, add Elements (such as Text Views) to present that data – you may want to initially set the Element(s) Hidden in the Properties tab.
  2. Open the request in the API tab on the right of the editor and navigate to Events. You should see an Event for each status code the request returns. error codes If you only see a successful code (e.g. 200 ), either your API doesn’t return any error codes, or your Swagger/OpenAPI spec doesn’t include all of the status codes returned from the endpoint.
  3. Open the error code Event you want to handle and click the plus plus button. You can use Actions to respond to the error, for example by running another request, or by updating the UI to indicate the error to the user.
    • To display an error message to the user, either:
      • Add a Set Value Action to display the info in the page, selecting an Element’s Text property in the first input.
      • Add a Show Alert / Show Alert Dialog to display a pop-up message.
    • In either Action, you can build data returned with the error status code response into your display, via the Event Data container. error code message What fields are available in the body of the response will depend on what the API returns along with the relevant code.
    • Complete the rest of your Action and Save it.
    • If you’re using Set Value to display text in an Element you’ve initially hidden, add another Set Value Action to set the Element Hidden property to a Static Input Boolean value of False , so that it shows in the page. show element on error

If you need to validate the response from an API request, for example to check the data returned with the 200 response, you can also do that using the request Events. For a working demonstration of API request errors in Action, check out the GitHub Example App when you create a new project.

Was this article helpful to you? Yes No

How can we help?