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
ignoreNotFound operator #1
Conversation
The operator can mark a resource as optional, so that when the server responds with the 404 status, the operator will safely return undefined instead. All other errors will be rethrown.
Hey Jan, thank you so much for the pull request, it looks well done! I'm just struggling a bit with the operator itself. I like the idea of encoding optional values but I don't think mapping a specific status to an undefined value is the best choice for two reasons:
Eventually, I'd propose to introduce a more generic operator called this.http.get<User>('/users/123').pipe(
recoverForCodes({
404: () => undefined // or rather: of(undefined)
})
) Otherwise, I'd also be open for a version of your operator, that completes instead of emitting |
Thanks for the reply. While While it's true that developers aren't always forced to handle it, they are immediately aware that I would also prefer I agree that an Optional type would be nice but too much as a dependency. We can also rename the operator. |
It seems as if Angular actually returns
I'd also argue, that without strict null checks, people assume any type to be potentially undefined, therefore I'd like to somehow work around
I see your point, but you could replace
Would you mind giving an example of the cases you'd use the operators for where a completion would be inconvenient? I'm not quite sure what you want to do with the undefined value, that you wouldn't rather do in e.g. My current proposal: let's postpone |
Sounds good to me. I have to admit I didn't know about
I was thinking about the
I think the point of the operator is to force a specific behaviour. Word it as a "recommendation" and it doesn't sound so bad anymore. That would be the trade-off here if you want to write less code / more concise code. I'm not sure how much of a benefit I'm not sure if I would remember additional operators if they do not provide that much in terms of LoC or semantic, especially when I already know how to use
This heavily depends on the developer's know-how and experience :)
Why exactly? If they expect things to be |
Previously the operator would always emit undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you still make a use-case example for the operator being added anyway? I'm not quite sure about its use-cases - irrespective of how it behaves. I don't see where ignoring a 404 error is different from letting it bubble through in an actual application. If you can't recover why keep on going?
Either way, I'd rename it to ignoreNotFound()
in compliance with ignoreElements
For example on an overview page for a user, where you want to show his car and his house. Your application has endpoints like this.userHouse$ = this.http.get<User>('/users/123/house').pipe(ignoreNotFound()); <app-house *ngIf="userHouse$ | async as houseData; else noHouse" [house]="houseData"></app-house>
<ng-template #noHouse><p>No house found</p></ng-template> I have used such an operator in a big application and it was really useful. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you name the file ignore-not-found.ts
(snake case) and the test file correspondingly? Right now it's optional.spec.ts
instead of ignore-not-found.spec.ts
I'll. merge after these changes
The operator can mark a resource as optional, so that when the server responds with the 404 status, the operator will safely return undefined instead. All other errors will be rethrown.