I make a request and want that only the nested requests are made when the first returns a http status code 303.
My backend returns 303 when session expired and I want that after successfull login the request for fetching person information is made again.
So is it possible to say that the nested request is only made when the request before has status code 303?
ok I try it… and thanks you for the detailed explanation.
It works fine except the fact that when I call the getPersons in my page and an error ocurred like 404, it goes in the success block and not in the error block which I except because Observable.throw(‘Something went wrong’) is thrown.
But I litte change your code… to classify the error messages…
ngOnInit(){
this.backend.readPersons().subscribe(
success => console.log(success),
error => {
console.log(error);
},
() => console.log('dismissed')
);
}
}
getPersons(): any {
// Requests persons from api
const getPersons = () => this.http.get(URL_PERSONS);
// First try to get persons
const response = getPersons();
// Stream emits when status 200
const result = response
.filter(res => res.status === 200)
.map(res => res.json());
// Stream emits when status 303
const notAuthorized = response
.filter(res => res.status === 303);
// Stream emits when status not 200 or 303
const failure404 = response
.filter(res => res.status === 404).map(()=>Observable.throw('404 occured'));
const failureOthers = response
.filter(res => res.status !== 404).map(()=>Observable.throw('Others occured'));
// Maps 303 to new request
const resultAfterAuth = notAuthorized
.mergeMap(res => this.login()) // mergeMap is the same as flatMap
.flatMap(getPersons);
// Combines all streams and returns the result as a stream
return Observable
.merge(
result,
resultAfterAuth,
failure404,
failureOthers
);
}
login(): Observable<boolean> {
return Observable.of(true);
}
}
const failureOthers = response .filter(res => res.status !== 404).map(()=>Observable.throw('Others occured'));
will have all responses that are not 404. Even 200 and 303. Is that, what you want?
You can also work with async await and promises, if you don’t like Observables:
async getPerson(): Promise<any> {
const sendRequest = () => this.http.get(URL_PERSONS).toPromise();
const response = await sendRequest();
switch(response.status) {
case 200:
return response.json();
case 401:
throw new Error('Unauthorized');
case 404:
throw new Error('File not found');
...
}
}
My problem is…
For each StatusCode I will show a user friendly message. It works fine now.
The only thing that does not works is to recognize Timeout errors.