-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
suggestion: consider improving the type signature for Observable.catch #2488
Comments
I'd be curious of usecase for 2 that instead of returning ErrorObservable throw directly, but otherwise this looks valid approach we'd like to improve. If you have PR, it'd be appreciate.
refer #2478 for this, afaik it's dealing this one. |
@kwonoj great to hear. Thanks for linking #2478. With respect to usecase 2, I had never actually seen it written that way until I read the code example in the JSDoc comment for the |
Also note that .catch(err => Observable.throw(err)) is 100% equivalent to .catch(err => {
throw err;
}) So the overloaded signature should not only return I met this a lot because I found a chain of .catch(err => {
span.setTag('error', true);
span.log({ 'event': 'error', 'error.object': err, 'message': err.message, 'stack': err.stack });
throw err;
})
.finally(() => {
span.finish();
}); easier to read (because they are closer to their synchronous counterparts and because of #2534) than .do(() => undefined, err => {
span.setTag('error', true);
span.log({ 'event': 'error', 'error.object': err, 'message': err.message, 'stack': err.stack });
})
.finally(() => {
span.finish();
}); Unfortunately, the former cripples the return type. |
@felixfbecker that's a great point. I also find that to be much more readable and it is mentioned in the JSDoc comment for the catch operator. Using |
RxJS version: 5.2.0
Code to reproduce:
case 1:
Observable<number>
decays toObservable<any>
case 2:
Observable<number>
decays toObservable<{}>
Expected behavior:
For
o
to have the typeObservable<number>
in both cases.Actual behavior:
o
has typeObservable<any>
in case 1.o
has typeObservable<{}>
in case 2.This can mask serious bugs such as
Where
f
should have typeObservable<void>
, likely due to programmer error, butany
absorbs evenvoid
.Additional information:
For better or worse, the catch and rethrow idiom seems to have overtaken RxJS, sometimes even when they only rethrow (not even logging the error) developers insist on explicitly catching and rethrowing all exceptions. I suspect the Angular docs are the primary reason for this, but that is beside the point except to remark that clearer usage guidelines for these operators would be helpful. Catching and only rethrowing doesn't make much sense to me but I am very new to RxJS.
For reference here is what the idiom I see daily (at work and on Stack Overflow) looks like
The problem with this pattern, and the pattern really is pervasive in the Angular at least, is that chaining
catch
removes all type information unless the user actually uses catch to recover by supplying a new Observable. If they return the result of anObservable.throw
call all further operations receiveObservable<any>
.To fix the type issue, I suggest that the signatures of
catch
and_catch
be changed fromto
This worked well when I tested it. I suppose the question is whether the idiom of catching and rethrowing is meant to be common for observables.
I would be happy to submit a PR for this if you are interested.
One final note, these could still arguably be more precise as the resulting stream from a catch returning a well-typed observable, e.g.
Observable<string>
when recovering from an error raised by anObservable<number>
is arguablyObservable<number | string>
but that is perhaps out of scope.Thank you.
The text was updated successfully, but these errors were encountered: