Swift macro library for automatic error type conversion.
Inspired by Rust crate thiserror.
@IntoError
enum AppError {
case network(URLError)
case parse(DecodingError)
// case unknown(any Error) — auto-generated if not declared
}
// Sync: use ^ operator
func fetchUser() throws(AppError) -> User {
let data = try URLSession.shared.data(from: url)^
let user = try JSONDecoder().decode(User.self, from: data)^
return user
}
// Async: use @Err macro
@Err
func fetchUserAsync() async throws(AppError) -> User {
let data = try await URLSession.shared.data(from: url)
let user = try JSONDecoder().decode(User.self, from: data)
return user
}- Swift 6.0+ / Xcode 16+
- macOS 10.15+ / iOS 13+ / tvOS 13+ / watchOS 6+
dependencies: [
.package(url: "https://github.com/tikhop/IntoError.git", from: "1.0.0")
].target(
name: "YourTarget",
dependencies: ["IntoError"]
)Attaches to enums and generates boilerplate code for you:
ErrorandErrorConvertibleconformance- Postfix
^operator (sync only) - Typed
init(from:)for each case init(converting:)with type-matching switchcase unknown(any Error)if no fallback case declared
@IntoError
enum MyError {
case specific(SomeError)
case other(AnotherError)
}Convert errors inline (sync only):
func fetch() throws(AppError) -> Data {
try networkCall()^
}Wrap function body for error conversion. Works with sync and async.
With typed throws:
@Err
func fetch() async throws(AppError) -> Data {
try await networkCall()
}With untyped throws:
@Err(AppError.self)
func fetch() async throws -> Data {
try await networkCall()
}See How It Works.
MIT