Contents

tikhop/intoerror

Swift macro library for automatic error type conversion.

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

Package Metadata

Repository: tikhop/intoerror

Default branch: master

README: README.md