How to ignore http response errors when call APIs angular

How to ignore http response errors when call APIs angular



How I can ignore HTTP response errors and return the response as is from API call in angular 5
I'm using angular HTTP client and this is my code for calling APIs



Api Handler:


get<T>(url:string):Observable<T>

return this.http.get<T>(url, this.getRequestHeaders()).pipe<T>(
catchError(error =>
return this.handleError(error, () => this.get(url));
));



Handle Error:


protected handleError(error, continuation: () => Observable<any>) {
if (error.status == 401)
if (this.isRefreshingLogin)
return this.pauseTask(continuation);

this.isRefreshingLogin = true;

return this.authService.refreshLogin().pipe(
mergeMap(data =>
this.isRefreshingLogin = false;
this.resumeTasks(true);

return continuation();
),
catchError(refreshLoginError => (refreshLoginError.url && refreshLoginError.url.toLowerCase().includes(this.loginUrl.toLowerCase())))
this.authService.reLogin();
return throwError('session expired');

else
));






what have you tried so far?

– DZDomi
Sep 16 '18 at 9:47






@DZDomi I updated my question, this is my code for catch error but I want if the status code is 404 or 400 return response that came from API as is

– Musab.BA
Sep 16 '18 at 10:16




1 Answer
1



If you want to continue your normal flow on an error code of 404 and 400 you just need to return a false Observable when you encounter this codes, like this:


import of from 'rxjs/observable/of';
...
protected handleError(error, continuation: () => Observable<any>) error.status == 400)
return of(false);

...






Thanks for the answer but when adding your code it gives me an error on (.of) Property 'of' does not exist on type 'typeof Observable'

– Musab.BA
Sep 16 '18 at 10:29






see updated answer

– DZDomi
Sep 16 '18 at 10:32






Thanks for the answer now (of) work but when checking data returned from API I found it equal (false)

– Musab.BA
Sep 16 '18 at 10:46






Thanks for the answer everything works perfectly now when return (of(error.error))

– Musab.BA
Sep 16 '18 at 13:10



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

How do I collapse sections of code in Visual Studio Code for Windows?