# Handle errors with http services

**URL:** <https://forum.ionicframework.com/t/handle-errors-with-http-services/53229>\
**Category:** ionic-v3\
**Created:** [May 27, 2016, 8:17pm UTC](https://forum.ionicframework.com/t/handle-errors-with-http-services/53229 "2016-05-27T20:17:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubiktubik](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@rubiktubik](https://forum.ionicframework.com/u/rubiktubik)\
**Post date:** [May 27, 2016, 8:17pm UTC](https://forum.ionicframework.com/t/handle-errors-with-http-services/53229/1 "2016-05-27T20:17:10Z")

</div>

Hello,

i’am using ionic 2 recently and its awesome!

But i have some problems on handling errors on http-services/providers which  
are created with the ionic cli generator.

**Service example:**

```
@Injectable()
export class TestService {
  user: User = null;

  constructor(public http: Http) {}
  

  load() {
    if (this.user) {
      // already loaded data
      return Promise.resolve(this.user);
    }

    // don't have the data yet
    return new Promise(resolve => {
      // We're using Angular Http provider to request the data,
      // then on the response it'll map the JSON data to a parsed JS object.
      // Next we process the data and resolve the promise with the new data.
      this.http.get('dummy/user.json')
        .map(res => res.json())
        .subscribe(data => {
          // we've got back the raw data, now generate the core schedule data
          // and save the data for later reference
          this.user = data;
          resolve(this.user);
        });
    });
  }

```

First question  
Why are the generated services expose a promise with “resolve” and not expose a observable?  
Is this the recommended way to create this kind of services?

Second question  
My usecase is to call a server and when the server is not available to  
show an alert.

```
this.TestService.load()
        .then(() => {
          //Success
        })
        .catch(err => {
            //show alert
        });

```

But when i try to call a server which is not available to test the alert behavior, on error it not  
goes to the catch case!

How can i handle http request errors properly to give users a feedback?

---

<div class="post-metadata">

**Author:** ![hugomosh](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hugomosh/32/75030_2.png) [@hugomosh](https://forum.ionicframework.com/u/hugomosh)\
**Post date:** [July 13, 2016, 10:23pm UTC](https://forum.ionicframework.com/t/handle-errors-with-http-services/53229/2 "2016-07-13T22:23:16Z")

</div>

Got the solution?  
I guess I will go for doing Observables

I did this:  
In the provider

```
 return this.http
     .post(this.baseURL + this.apiVersion + 'users/login', body, options)
      .map(res => res.json())
      .toPromise();

```

In the service:

```
this.api.login(loginForm)
    	.then(data => {
   		console.log(data);
    	})
 	.catch(error => { console.log(error) });

```

Hope it works for others. Dont know if it is the right way.

---

<div class="post-metadata">

**Author:** ![itlr](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/itlr/32/97646_2.png) [@itlr](https://forum.ionicframework.com/u/itlr)\
**Post date:** [July 13, 2016, 11:48pm UTC](https://forum.ionicframework.com/t/handle-errors-with-http-services/53229/3 "2016-07-13T23:48:08Z")

</div>

subscript takes 3 callbacks, success, fail, complete

```auto
subscribe(
  (value) => ... // success,
  (err) => ... // fail
  () => ... // complete
)

```

---

<div class="post-metadata">

**Author:** ![ZetaGas](https://avatars.discourse-cdn.com/v4/letter/z/c37758/32.png) [@ZetaGas](https://forum.ionicframework.com/u/ZetaGas)\
**Post date:** [August 12, 2017, 1:50pm UTC](https://forum.ionicframework.com/t/handle-errors-with-http-services/53229/4 "2017-08-12T13:50:52Z")

</div>

.subscribe(response =\> {  
…  
},onerror=\>{ ‘Error menssage’});
