Reputation: 428
How can I make this work?
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 );
Not passing the function and the value as separate arguments is the whole point.
For example with NgRx with an Effect you return:
map(() => new SuccessAction()),
catchError((value: number) => new FailureAction(value))
I want to do the same as that catchError, but using a custom handler.
Also, it must handle the error without affecting the success case, this is going to be passed inside the Effects pipe and the action returned will be different for success and failure.
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))),
On an Effect:
.pipe(
map(() => new AuthLoginSuccess()),
effectsErrorHandler((counter: number) => new AuthLoginFailure(counter)),
);
Based on the link provided by @martin, this worked:
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))),
),
);
But isn't there a way to just wrap the catchError like in my example instead of having to wrap everything like here?
Upvotes: 1
Views: 2169
Reputation: 428
This is what I was looking for:
export const effectsErrorHandler = (failureAction: ActionCreationFunction) =>
(source: Observable) =>
source
.pipe(
catchError(err =>
of(failureAction(err))
.pipe(tap(() => console.log(err))),
),
);
Now I can extend it for my usecase
Upvotes: 3