2

I'm trying to catch HTTP error with error code 400 using the following code in a ionic4 application. But it fails to catch it, what's wrong in here. None of the console log lines executes, but in firefox console it shows 400 error.

export class AuthService {

  constructor(private httpClient: HttpClient)  {}

  login(email: string, password: string){

    const data = {
      password: password,
      email: email,
    }

    this.httpClient.post(url, data).pipe(
      tap((res: IAuthResponse) => {
        console.log("Catch error 1") 
        return res;
      }),
      catchError((error) => {
        console.log("Catch error 2")
        return Observable.throw(new Error(error.status));
      })
    ).subscribe( 
       (result) => {
         console.log("Catch error 3")
       },
       (error) => {
          console.log("Catch error 4")
       }
    );
  }

enter image description here

Actually i just want to handle this error in any of the place where i coded console.log line. Found many examples like this, but none of them works.

Edit : In my real code this, console log line 1 is in the AuthService class but the subscribe code is in a different class file. Both these classes has to do some initialization based on the results. So I need to have both of pipe code and subscribe code.

4 Answers 4

7

this is the way that can be done

Here is working Example

this.httpClient.post("ssdsdsd", data).pipe(
      (res: any) => {
        console.log("Catch res ") 
        return res;
      },
    ).subscribe( 
       (result) => {
         console.log("Catch result ")
       },
       (error) => {
          console.log("Catch error ")
       }
    );
Sign up to request clarification or add additional context in comments.

Comments

3

No need to use pipe in httpClient You can try like this:

this.httpClient.post(url, data).subscribe((res: any) => {
}, (error) => {
  console.log("Catch error ")
})

1 Comment

Thank you for the answer. I think my question is not detail enough. In my real code this, console log line 1 is in the AuthService class but the subscribe code is in a different class file. Both these classes has to do some initialization based on the results.
2

What you are trying to achieve is The Catch and Rethrow Strategy, as explained here in details. Its pretty straightward implementation, no matter if your pipe-catchError & subscription exists in same class or different, I can't think of a reason why your code is acting weird. I would say put debug points in your code and debug in chrome/firefox developer mode.

const http$ = this.http.get<Course[]>('/api/courses');

http$
    .pipe(
        catchError(err => {
            console.log('Handling error locally and rethrowing it...', err);
            return throwError(err);
        })
    )
    .subscribe(
        res => console.log('HTTP response', res),
        err => console.log('HTTP Error', err),
        () => console.log('HTTP request completed.')
    );

Comments

1

@Sameera.. you can catch the error (with status code) something like below,

this.httpClient.post(url, data, {observe: 'response'}).subscribe((res: IAuthResponse) => {
        console.log(res);
        return res;
   }, fail => {
          console.log(fail.status);
       }
   );

you don't need to use any pipe or other operators for this..

you can check more about this here httpClient response with Status code. hope this helps.. :)

1 Comment

Thank you for the answer. I think my question is not detail enough. In my real code this, console log line 1 is in the AuthService class but the subscribe code is in a different class file. Both these classes has to do some initialization based on the results

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.