Require catch block on EffectPublisher.task
s that throw
#1847
Replies: 2 comments 9 replies
-
👍 I just converted a non-throwing .run { send in
for try await response in await self.vesperTemplateClient.fetchTemplates() {
await send(.fetchTemplatesResponse(.success(response)))
}
} catch: { error, send in
await send(.fetchTemplatesResponse(.failure(error)))
} Somewhat related, I typically use the block initializer for await send(.promotionalOffersResponse(TaskResult {
try await self.upgradesClient.promotionalOffers()
})) |
Beta Was this translation helpful? Give feedback.
-
Hi @patch-benjamin, thanks for starting this discussion! I have a few questions for you:
We currently find that the balance of not requiring Our main concern with adopting something like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've noticed on a few PRs at work recently that there is a potential for hanging UI bugs to occur with the optional provision of a catch block on
EffectPublisher.task
.I propose two small (breaking) changes to EffectPublisher:
EffectPublisher.task
to not provide a defaultnil
value for the catch block.EffectPublisher.task
function that doesn't require a catch block.The result of these changes would be to make it clearer to developers that errors are thrown and allow them to explicitly specify how they want to handle those thrown errors -- even if that is
catch: nil
.This may be a moot suggestion with the direction of TCA moving away from Combine, but figured I'd throw it out there for discussion at the least since I assume similar functionality will be available in the next major version.
If you don't see how a thrown error could leave the UI in a hanging state, here's an example from our code base:
Loading
-> Result: The user gets stuck in a hanging Loading state rather than an error state
I know the dev has ultimate responsibility for handling the catch block when he calls
try
in his code, but I like the idea of making it more in your face that an error could be thrown and that you are ignoring the catching of that error.The other benefit of this change is that users can extend
EffectPublisher
and implement their own version offunc task
to give the catch block a default value if they want. Whereas the current approach does not allow users to remove that default value in an effort to force developers to explicitly define the catch block.Here's what the code change to the API could look like:
And then using a throwing task would change from:
to
Thus forcing developers to specify that they don't want to do anything for the catch block.
I also wouldn't be opposed to taking it one step further and making the catch block non-optional but returning an optional
Action?
like so:which would require the usage to look like:
That would make it even more clear that you are intentionally not returning an Action and you understand the consequences (potential hang state in the UI).
Beta Was this translation helpful? Give feedback.
All reactions