Skip to content

derive(any Error) for enum error types

License

Notifications You must be signed in to change notification settings

tikhop/IntoError

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IntoError

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
}

Requirements

  • Swift 6.0+ / Xcode 16+
  • macOS 10.15+ / iOS 13+ / tvOS 13+ / watchOS 6+

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/tikhop/IntoError.git", from: "1.0.0")
]
.target(
    name: "YourTarget",
    dependencies: ["IntoError"]
)

API

@IntoError

Attaches to enums and generates boilerplate code for you:

  • Error and ErrorConvertible conformance
  • Postfix ^ operator (sync only)
  • Typed init(from:) for each case
  • init(converting:) with type-matching switch
  • case unknown(any Error) if no fallback case declared
@IntoError
enum MyError {
    case specific(SomeError)
    case other(AnotherError)
}

^ Postfix Operator

Convert errors inline (sync only):

func fetch() throws(AppError) -> Data {
    try networkCall()^
}

@Err

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()
}

How It Works

See How It Works.

License

MIT

About

derive(any Error) for enum error types

Resources

License

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages