I am trying to log message from backend where if I give wrong otp number it will throw status as 200ok and with error message in response body.
I want to log that error message ErrorMsg so that I can use it in some alert.
But in my case I am getting message which I have written in HttpErrorResponse in service
// Handle errors
handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
console.error('An error occurred:', error.error.message);
} else {
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${error.error}`);
}
return throwError(
'Something bad happened; please try again later.');
}
So I am getting Something bad happened; please try again later in error response. How I can change this to the one I am getting in POSTMAN ?
Service login:-
//POST API for Login
loginForm(data: any): Observable<RootObject> {
return this.http
.post<RootObject>(`${environment.baseUrl}/VerifyMobileOTP`, data, this.httpOptions)
.pipe(
tap((resp: RootObject) => this.setUser(resp.Result as Result)),
catchError(this.handleError)
);
}
setUser(resp: Result) {
this.storageService.secureStorage.setItem('username', JSON.stringify(resp.username));
this.storageService.secureStorage.setItem('access_token', JSON.stringify(resp.access_token));
this.router.navigate(['/home']);
}
Model:-
export interface Result {
access_token: string;
username: string;
}
export interface RootObject {
IsSuccess: boolean;
ErrorMsg: string;
Result: Result;
ErrorCode?: any;
}
Login Component :-
login() {
this.loading = true;
var number = this.loginForm.value[Object.keys(this.loginForm.value)[0]]
this.otp = this.otpForm.value[Object.keys(this.otpForm.value)[0]]
console.log(number, this.otp);
var loggedin = { "MobileNumber": number, "OTP": this.otp }
// call login api
this.authService.loginForm(loggedin).subscribe(response => {
console.log(response);
this.errorLogin = response.ErrorMsg;
console.log(this.errorLogin); // ------> How to console 200ok response error ?
this.loading = false;
}, error => {
this.loading = false;
this.status = error;
console.log(error) // ------------>>>> this error msg I am receiving from httperrorresponse
});
}
EDIT:-
On consoling response I am getting value from Result model only and not the value from model RootObject which content ErrorMsg and IsSucess values
{
access_token: fdgfefeWD45dfdS ....;
username: test@wren.com;
}
