Is it possible to make early exit in do-try-catch block? (Like `guard`)

There have been some pitches for this guard catch syntax and I think it would be extremely valuable to swift. For my use case I am using SwiftNio and as recommended I want to return failed futures instead of throwing errors. I want to do something like:

guard let encodedJson = try JSONEncoder().encode(myCodableObject) catch {
    // return failed eventLoopFuture containing error
    return group.next().makeFailedFuture(error)
}
...

Unfortunately without guard/try/catch my code is much more verbose

var encodedJSONOptional: Data? = nil
do {
    encodedJSONOptional = try JSONEncoder().encode(myCodableObject)
} catch {
     return group.next().makeFailedFuture(error)
 }
guard let encodedJSON = encodedJSONOptional else {
    return group.next().makeFailedFuture(SomeError.couldNotEncodeAsJSON)
}
...
3 Likes