Search code examples
angularrestjerseyionic4

How to manage backend connection errors on Ionic 4 / angular


I'm developing Ionic 4 application based on Angular.The application consumes REST API provided by Java application.

The application can call the backend and get the data, the problem is when there is an issue with connectivity (internet problem, wrong server adress..) In this case i can access the application normally, but everything is empty

I want the application to be able, during the run-time, to check the connectivity to the backend server, and show a warning message to the user with a try again option, if the connection to the server has failed. Otherwise fetch the data from the server.

Thanks in advance


Solution

  • If I understand correctly, what you want to do is basically call your backend REST service from the ionic app using httpClient. For that you will have to import the HttpClientModule in app.module.ts .

    Then, you can inject the httpClient into an angular service and create a method that will call your API endpoint.

    Then, whenerver you need it, you simply subscribe to this method and supply two callbacks:

    • next callback - on success this code will be called and ypou can access your data from your API

    • error callback - this will be called if the server responds with an error. You can put the code for displaying an error message here.