import ArgumentParser import Foundation import SprachmittlerCore import SprachmittlerOllama enum CLIError { static func perform(_ operation: () async throws -> Void) async throws { do { try await operation() } catch let exit as ExitCode { throw exit } catch let error as OllamaError { writeError(message(for: error)) throw ExitCode(exitCode(for: error)) } catch let rejection as EndpointRejection { // Defensive: EndpointPolicy may be called directly outside OllamaHTTPClient. writeError(message(for: rejection)) throw ExitCode(2) } catch let validation as ValidationError { writeError(String(describing: validation)) throw ExitCode(2) } catch let domain as SprachmittlerError { writeError(message(for: domain)) throw ExitCode(exitCode(for: domain)) } catch { writeError("Error: \(error)") throw ExitCode(1) } } /// 2 = endpoint/usage, 3 = unreachable, 4 = model missing, 1 = other runtime failure. static func exitCode(for error: SprachmittlerError) -> Int32 { switch error { case .endpointRejected, .noAdapter, .emptyText, .textTooLong, .badRequest: 2 case .ollamaUnreachable: 3 case .modelNotInstalled: 4 default: 1 } } static func message(for error: SprachmittlerError) -> String { switch error { case let .ollamaUnreachable(url): "Ollama is not reachable at \(url.absoluteString)." case let .modelNotInstalled(model): "Model not installed: \(model)" case let .noAdapter(model): "No translation adapter for \(model)." case let .endpointRejected(reason): "Endpoint rejected: \(message(for: reason))" default: genericMessage(for: error) } } private static func genericMessage(for error: SprachmittlerError) -> String { switch error { case .emptyText: "No text to translate." case let .textTooLong(characters, limit): "Text is too long (\(characters) characters; limit \(limit))." case let .timeout(phase): "Timed out while talking to Ollama (\(phase))." case let .server(status, _): "Ollama returned HTTP \(status)." case .badRequest: "Ollama rejected the request." case .cancelled: "Cancelled." default: "Translation failed." } } /// 2 = endpoint/usage, 3 = unreachable, 4 = model missing, 1 = other runtime failure. static func exitCode(for error: OllamaError) -> Int32 { switch error { case .endpointRejected: 2 case .unreachable: 3 case .modelNotInstalled: 4 default: 1 } } static func message(for error: OllamaError) -> String { switch error { case let .endpointRejected(reason): "Endpoint rejected: \(message(for: reason))" case let .unreachable(url): "Ollama is not reachable at \(url.absoluteString)." case let .modelNotInstalled(model): "Model not installed: \(model)" case .badRequest: "Ollama rejected the request." case let .server(status, _): "Ollama returned HTTP \(status)." case .timeout: "Timed out while talking to Ollama." case .cancelled: "Cancelled." case .decoding: "Ollama returned an unreadable response." case let .unsupportedVersion(found, required): "Ollama \(found) is too old; \(required) or newer is required." } } static func message(for rejection: EndpointRejection) -> String { switch rejection { case .scheme: "Only http and https endpoints are supported." case .form: "The endpoint must not contain credentials, a query or a fragment." case .remoteNotAllowed: "Remote endpoints are disabled; use --allow-remote or a loopback endpoint." case .remoteRequiresHTTPS: "Remote endpoints require HTTPS." case .redirect: "The endpoint redirected; redirects are refused." } } /// Diagnostics go to stderr so `translate --json` keeps stdout machine-readable. private static func writeError(_ message: String) { FileHandle.standardError.write(Data((message + "\n").utf8)) } }