Reputation: 11
I have an issue with catching error in Ionic based with Angular.
I am on create() method trying to create new User and if username already exists, i retrieve response from backend but on my method error throws the named message in title. I have tried most of the similar answers but still stuck
any help
Config.ts
import {HttpErrorResponse} from '@angular/common/http';
import {Observable, throwError} from 'rxjs';
export default class Config {
static handleError(error: HttpErrorResponse): Observable<any> {
console.log('*****handleErrors*****');
console.log(error); //TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
console.log(error.message); //TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
console.log(error.error.message);
return throwError(
error.error.message());
}
}
account.service.ts
create(account: Account): Observable<Account> {
return this.httpClient
.post<ResponseWrapper>(this.accountsUrl, JSON.stringify(account), this.httpOptions).pipe(
map(rw => {
return rw.data;
}),
catchError(
this.handleError
)
);
}
handleError(error: Response | any) {
return Config.handleError(error);
}
account-detail.page.ts, I didnt want to spam to much code with toastService I have created, but toastService is working
this.accountService.create(this.selectedAccount).subscribe(
res => {
this.selectedAccount = res;
const io = new InteractionObject('save', 'account', this.selectedAccount);
this.accountDetailEvent.emit(io);
this.interactionService.setSave(io);
setTimeout(() => {
}, 500);
this.toastService.showSaveSuccessMsg();
},
error => { //TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
this.errorMsg = error;
if ('ACCOUNT_USERNAME_EXISTS' === this.errorMsg) {
this.toastService.showSaveFailMsg('account_username_exists');
}
}
);
Upvotes: 1
Views: 10779
Reputation: 691
If you have no data in the response, your map won't be able to return an Account. You will not get into to catch if the request is "OK".
To fix it, you could add a check on the response and if no account is found then, you can try to throw an exception
Try to add logs to see if your rw.data is set
Upvotes: 2