7

I have achieved to get the value of the API error return via catch in my HTTP Request. My problem now is how can I get the return value of the HTTP catch in my component when I call the service.

This is my code in my HTTP service:

login(username,password){
        let headers = new Headers();
        headers.append('Content-Type','application/json');

        return this.http.post(this.configEnvironment.url() + "oauth/access_token",
            JSON.stringify(
                {
                    username: username,
                    password: password,
                    grant_type: "password",
                    client_id: "xxxx",
                    client_secret: "xxxxx"
                }
            ),
            { headers }
        )
        .map(res => res.json())
        .catch((err:Response) => {
            let details = err.json();
            return Observable.throw(new Error(details));
         });

     }

This is my code in my login.component:

this.loginService.login(this.model.username,this.model.password)
            .subscribe(
                data => console.log("data"),
                error => {
                   console.log(error);
                },
                ()  =>  console.log("Finished")
            );

and in the chrome developer tools this is the return of the console.log:

Error: [object Object](…)

But the actual return of the http service catch is this: { "error":"invalid_credentials","error_description": "invalid credentials" } and this is I want to get in the login.component

3
  • I don't understand. What you get in the console is not what you want? What would you want instead? Commented Oct 16, 2016 at 7:19
  • The actual return of the service is this: {"error":"invalid_credentials","error_description": "invalid credentials" and this i I want to get in the component Commented Oct 16, 2016 at 7:26
  • try error.error_description in the component. Commented Oct 16, 2016 at 7:30

1 Answer 1

8

In your .catch(), change:

return Observable.throw(new Error(details));

to:

return Observable.throw(details);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.