where to put toaster on http service calls typescript

The solution for “where to put toaster on http service calls typescript” can be found here. The following code will assist you in solving the problem.

@Injectable()
export class HttpInterceptor implements HttpInterceptor {
constructor(public toasterService: ToastrService) {}

intercept(
req: HttpRequest,
next: HttpHandler
): Observable> {

return next.handle(req).pipe(
tap(evt => {
if (evt instanceof HttpResponse) {
if(evt.body && evt.body.success)
this.toasterService.success(evt.body.success.message, evt.body.success.title, { positionClass: ‘toast-bottom-center’ });
}
}),
catchError((err: any) => {
if(err instanceof HttpErrorResponse) {
try {
this.toasterService.error(err.error.message, err.error.title, { positionClass: ‘toast-bottom-center’ });
} catch(e) {
this.toasterService.error(‘An error occurred’, ”, { positionClass: ‘toast-bottom-center’ });
}
//log error
}
return of(err);
}));
}
}

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in