withTmuxError(_:)
Swift
- Python Unavailable
- Ruby Unavailable
- Lua Unavailable
- TypeScript Unavailable
- Rust Unavailable
- Go Unavailable
- Java Unavailable
- .NET Unavailable
- C++ Unavailable
- Swift
- Package
- libtmux-swift
- Source
- Sources/LibTmux/TypedErrorScope.swift
-
Narrowing a scope's thrown type back to
TmuxError.Every call in this library throws
TmuxErrorand says so, which lets a program writethrows(TmuxError)from top to bottom. The scoped forms —Server/using(_:_:),Server/connected(attachingTo:_:)andServer/withControlMode(attachingTo:_:)— are the exception: they take a closure, and a closure's thrown type cannot be carried out of one.That is a property of the language rather than a choice made here. Swift 6.2 does not infer a closure literal's thrown type from its body, so a
throws(TmuxError)overload of those scopes is unreachable without spelling the closure's own signature at every call site — and even with the type inferred, a scope that can fail on its own behalf has no way to rethrow that failure as the body's error, because there is no union of the two.So the narrowing happens at the boundary instead of inside the scope:
One wrapper per scope, rather than an annotation per closure.
- Important: This is for work that only fails with
TmuxError. Anything elsebodythrows is flattened intoTmuxError/invocationFailed(reason:)with its description as the reason, which keeps the signature honest but loses the original type. A body that throws errors of its own should stay untyped and be caught as itself. - Parameter body: the work to run, typically a scoped call.
- Returns: whatever
bodyreturned. - Throws:
TmuxError, either as thrown or as a flattened foreign error.
Examples
func names(_ server: Server) async throws(TmuxError) -> [String] {try await withTmuxError {try await server.using(.connected(to: "main")) { server intry await server.sessions().map(\.name)}}}- Raises
- Important: This is for work that only fails with
0 declared, 0 inherited