Closed elielamora closed 5 years ago
Hi Eliel, thank you for raising the issue!
Are you looking to make validate's predicate 'throwable'? If so, do you mind adding a code snippet demonstrating what you had in mind?
Which cases would the predicate return 'false' vs. throw an error?
Hi Eliel, please feel free to open if this is still an issue. Thank you!
.validate
returns identity if the predicate is true
. If the predicate is false
it throws a fixed exception FBLPromiseErrorCodeValidationFailure
which I initially wanted to be overridable by a user to allow a custom subclass of NSError
to be thrown. This would be in hopes of separating the promise chain between the action to perform and the error handling logic.
I was able to accomplish this with a helper function leveraging .recover
to catch that exception and rethrow the custom exception. Similar to what is shown below.
func validate(
on queue: DispatchQueue,
_ error: Error,
_ predicate: Predicate<T>
) throws -> Promise<T> {
return self
.validate(on: queue, predicate)
.rethrow(on: queue, error)
}
I then realized that it is more idiomatic to use the library in the following way:
Promise<Int>(3)
.validate{$0>5}
.catch{/* handle this error */}
.validate{$0%2 == 0}
.catch{/* handle this other error */}
Where the catch following each validate knows the 'type' of error needed to handle.
I will be closing the issue as the proposed changed doesn't need to be a part of the core library and there are other ways of accomplishing the same thing.
It would be nice if
validate
allowed a custom error to be thrown if the predicate was false.There is no way to discern what validation failure when there are multiple in a chain.
This can arise when there are reusable promise subchains (each with its own prerequisites) which are arbitrarily arranged. When these are grouped there is typically one large catch-all at the end.
Something like the following would simplify the logic and make better use of the library.