I tried to throw an observable error in the interceptor and then handles this error. but I can\'t. When success is false, I can also receive successful subscribe.
I
Try doing this in your interceptor
return next.handle(request).map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse && !event.body.success) {
throw new Error(event.body.message);
}
return event;
);
Try doing this in your interceptor
return next.handle(request).map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse && event.body && !event.body.success) {
throw new HttpErrorResponse({
error: 'your error',
headers: evt.headers,
status: 500,
statusText: 'Warning',
url: evt.url
});
}
return event;
);
You can add catch, and handle error
//....
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/map';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(private inj: Injector, private _router: Router) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Get the auth header from the service.
const auth = this.inj.get(AuthService);
const authToken = auth.getSessionData().accessToken || '';
const duplicate = req.clone({headers: req.headers.set('Authorization', authToken)});
return next.handle(duplicate).catch(this.handleError);
}
private handleError = (response: any) => {
// ....
// Do messaging and error handling here
return Observable.throw(response);
}