Fallback
Fallback copied to clipboard
Add support chaining
This is for whom wants handle each errors.
From the comment from Reddit:
Keeps it short and sweet. My biggest concern is that it's limiting errors to control flow. If get("A") and get("B") fail and we get("C") instead, what happened to the errors from the previous 2 calls? It looks to me like it only throws the last error.
Examples
let value: String = fallback {
return try get("a")
}.catch { error in // error from "a"
return try get("b")
}.catch { error in // error from "b"
return try get("c")
}.finally { error in // error from "c"
return "none"
}
print(value) // "none"
let value: String = try fallback {
return try get("a")
}.catch { error in // error from "a"
return try get("b")
}.catch { error in // error from "b"
return try get("c")
}.rethrow() // will throw error from "c"
To Do
- [x] Implement
- [x] Test
- [ ] Documentation