Вот идея избежать «вложенной» подписки. С предоставленным вами кодом, это лучшее, что я могу придумать. Если у вас есть какие-либо вопросы, дайте мне знать, и я помогу.
import { of, throwError } from 'rxjs';
import { map, switchMap, catchError } from 'rxjs/operators';
// this.requestService().subscribe(
// () => this.success(),
// error => {
// const errorDescription = {
// one: 5,
// two: 10
// };
// return this.error(errorDescription).subscribe();
// }
// );
const error = throwError('oops');
const success = of('success!');
const handleError = (d) => of({one: 5, two: 10}).pipe(
map(n => n),
catchError(e => 'could not do 2nd network request')
);
const requestServiceSuccess = success.pipe(
switchMap(d => of(d)),
catchError(handleError)
)
const requestServiceFail = error.pipe(
switchMap(d => of(d)),
catchError(handleError)
)
// if your first network request succeeds, then you will see success
requestServiceSuccess.subscribe(
(d) => console.log(d),
(e) => console.log(e)
)
// if your first network request fails, then you will see your errorDescription
requestServiceFail.subscribe(
(d) => console.log(d),
(e) => console.log(e)
)
Вы можете вставить это в stackblitz, чтобы проверить журналы
https://stackblitz.com/fork/rxjs?devtoolsheight=60