google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.8k stars 294 forks source link

How to catch all error? #116

Closed EddieLukeAtmey closed 5 years ago

EddieLukeAtmey commented 5 years ago

When fusing all(), probably we want all our request to be successful. If anyone failed, It'll be invalid. Then we'll have our users to do something to make it valid again. So to save request, we'd want the users to apply all the necessary condition in order to complete the request, by showing all the error in all the required request. How can we do so?

Even if I use await(), it's still no use because it still throws the first error immediately.

shoumikhin commented 5 years ago

Hi @EddieLukeAtmey,

Sounds like you may need to add a recover clause to each promise you're passing to all. There's also retry if you can effectively recover a promise by retrying it.

Or maybe you can use any in stead of all and post-process the errors somehow, or simply report them and do not retry at all.

It's hard to say w/o understanding your particular use case deeper.

Thanks.

EddieLukeAtmey commented 5 years ago

Hi @shoumikhin, thanks for the response.

My use case is like this:

I have 3 Requests: A, B, C. In order to complete the task in MyVC, they'll have to finish all the required conditions to request A, B, and C. The requests will be called simultaneously, and response when all done. That's why I'd use all().

However, the catch() only catch either one error in A, B, or C; Therefore I can't show all the errors to the users. If they failed all the conditions, they'd have to make the requests 3 times (A failed -> B failed -> C failed) * 3 req = 9 requests in total. That's a waste of resource, so I'd like to show all the errors at once if possible.

As you suggest, recover would solve the problem but it'll also create a lot of boilerplate code (each request make a recover() to resolve with an error msg or something and append it. But then, it's no much use for the catch().

Or if you could suggest any better way to solve this, thank you.

shoumikhin commented 5 years ago

Why a request can fail? How do you recover it? Do you just retry it a few times and then given up?

Hopefully, you can factor out the request creation into a separate helper function and put the recover code over there to avoid boilerplate.

So you can use retry or recover for each of those 3 requests, and then leverage any instead of all to get all failed requests, despite you've tried to recover them.

EddieLukeAtmey commented 5 years ago

You're right, I'd better refactor the error handling in the ServiceRequester instead. We need 3 Promises, one of them is a lie, all of them are lies. If we want to catch() all lies, better merge 3 Promises into a bigger Promise and catch the error for only once.