Clean, expressive error handling for Swift.
TryKit makes it painless to work with Swift’s Result
type by providing chainable helpers like .onSuccess
, .recover
, .flatMap
, and more — including tryCatch
wrappers for both sync and async functions.
Writing Swift code that deals with errors doesn't need to look like this:
do {
let data = try fetchData()
let value = try transform(data)
print("Success:", value)
} catch {
print("Error:", error)
}
With TryKit, you can streamline your logic:
tryCatch { try fetchData() }
.flatMap { data in tryCatch { try transform(data) } }
.onSuccess { print("Success:", $0) }
.onFailure { print("Oops:", $0.localizedDescription) }
Add this to your Package.swift
:
.package(url: "https://github.com/jpmcglone/TryKit.git", from: "1.0.0")
And add "TryKit"
to your target dependencies.
Or in Xcode:
- Go to
File → Add Packages…
- Enter
https://github.com/yourusername/TryKit
- Choose your version and add it to your target
import TryKit
let result = tryCatch {
try fetchUserProfile()
}
result
.onSuccess { user in
print("Welcome, \(user.name)")
}
.onFailure { error in
print("Error loading profile:", error)
}
Each transformation only runs if the previous step succeeded:
tryCatch { try loadUserID() }
.flatMap { id in tryCatch { try fetchUser(id: id) } }
.map { $0.name }
.recover { _ in "Guest" }
.onSuccess { print("Hello, \($0)") }
- ✅
tryCatch { ... }
for sync and async - ✅
.onSuccess { ... }
and.onFailure { ... }
- ✅
.map { ... }
and.flatMap { ... }
- ✅
.recover { ... }
and.recoverWith { ... }
- ✅
.isSuccess
and.isFailure
helpers - ✅ Fully tested and lightweight
await tryCatch {
try await loadUsername()
}
.onSuccess { name in
print("Welcome, \(name)")
}
.onFailure { error in
print("Failed to load user:", error)
}
let result = await tryCatch {
try await fetchRemoteSettings()
}
let settings = result
.map { $0.theme }
.recover { _ in "light" }
print("Theme:", settings)
let result = tryCatch {
try loadUsername()
}
.recover { _ in "Guest" }
print("Hello, \(result)")
tryCatch { try fetchToken() }
.flatMap { token in
tryCatch { try fetchUser(token: token) }
}
.map { $0.name }
.onSuccess { print("Welcome, \($0)") }
.onFailure { print("Login failed") }
Every method is fully documented with Swift-style doc comments.
You can also use Option + Click
in Xcode for inline docs.
To run tests:
swift test
Covers:
- Sync and async flows
- Mapping and flatMapping
- Recovering from errors
- Chaining and branching behavior
Built with care by JP McGlone
Inspired by Swift’s powerful Result
type and functional patterns.
MIT License. See LICENSE for details.