我该如何做?
switchMap((value: number) => throwError(value)
.pipe(
customeErrorHandler((value: number) => value * 3)
);
);
export const customErrorHandler = (function: Function) =>
catchError() => of( the function passed as parameter evaluated with the value );
重点是不传递函数和值作为单独的参数。
例如,使用具有效果的NgRx,您将返回:
map(() => new SuccessAction()),
catchError((value: number) => new FailureAction(value))
我想执行与catchError相同的操作,但要使用自定义处理程序。
另外,它必须在不影响成功案例的情况下处理错误,这将在Effects管道内传递,并且对于成功和失败,返回的操作将有所不同。
f.e。:
type ActionCreationFunction = (payload: any) => Action;
export const effectsErrorHandler = (failureAction: ActionCreationFunction) =>
catchError((err: any): Observable<Action> =>
of(failureAction(payload))
.pipe(tap(() => console.log(err))),
关于效果:
.pipe(
map(() => new AuthLoginSuccess()),
effectsErrorHandler((counter: number) => new AuthLoginFailure(counter)),
);
根据@martin提供的链接,此方法有效:
export const effectsErrorHandler = (failureAction: ActionCreationFunction) =>
source =>
Observable.create(subscriber =>
source.subscribe(
value => subscriber.next(value),
err => subscriber.next(failureAction(err))
.pipe(tap(() => console.log(err))),
),
);
但是有没有办法像我的示例中那样包装catchError而不是像这里那样包装一切?
答案 0 :(得分:1)
这就是我想要的:
export const effectsErrorHandler = (failureAction: ActionCreationFunction) =>
(source: Observable) =>
source
.pipe(
catchError(err =>
of(failureAction(err))
.pipe(tap(() => console.log(err))),
),
);
现在我可以将其扩展为我的用例