In Core there is an extension to Future<OptionalType> adding an unwrap(or:) method that either returns the value if it's not nil or throws the error provided to the function.
This is really useful, but there is no method to do this the other way around. For example, if you're working with a database where a certain key is unique you might not want to rely on the datbase to error, but catch it yourself beforehand with some code like this:
User.query(on: req).filter(\.email, .ilike, providedEmail).first().map { optionalUser in
guard optionalUser == nil else {
throw Abort(.badRequest, "user with this email already exists")
}
// Continue ...
}
Proposal
Add a nonExistant(or:) function to Future<OptionalType> to allow for flows like this:
User.query(on: req)
.filter(\.email, .ilike, providedEmail)
.first()
.nonExistant(or: Abort(.badRequest, "user with this email already exists"))
.map {
// Continue ...
}
In
Core
there is an extension toFuture<OptionalType>
adding anunwrap(or:)
method that either returns the value if it's notnil
or throws the error provided to the function.This is really useful, but there is no method to do this the other way around. For example, if you're working with a database where a certain key is unique you might not want to rely on the datbase to error, but catch it yourself beforehand with some code like this:
Proposal
Add a
nonExistant(or:)
function toFuture<OptionalType>
to allow for flows like this: