hex/ios/HEXShared/UserFacingError.swift
Ryan Vogel 882d5412be iOS: retry '!int' audio activation mixable and show audio errors as sentences
TestFlight testers saw "The operation couldn't be completed. (OSStatus error
560557684.)" in the keyboard strip and the app's setup card. 560557684 is
'!int' (AVAudioSession cannotInterruptOthers): a non-mixable session (the
Bluetooth-microphone .record category) going active from the background while
another app plays or records.

- AudioBackend: when setActive(true) fails with '!int', set .playAndRecord
  with .mixWithOthers (keeping HFP when Bluetooth is allowed) and activate
  once more, so the warm keepalive and captures start alongside the other
  app's audio instead of failing.
- New UserFacingError (HEXShared, app and keyboard): AVAudioSession codes map
  to short sentences ("Another app is using audio. Try again."); other
  microphone errors get "Hex couldn't start the microphone. Try again.";
  transcription and model errors keep their description only when it reads
  as a sentence, never Foundation's "couldn't be completed (… error N)" or a
  Swift type name.
- DictationController: every error it shows (phase .failed and the
  keyboard's message) goes through UserFacingError instead of
  localizedDescription.

Verified: xcodebuild test on the iPhone 18 Pro simulator, 179 tests pass,
including new tests for the code-to-sentence mapping and for a warm session
whose keepalive throws '!int' reaching the app and keyboard snapshot as
"Another app is using audio. Try again.". The background '!int' retry itself
needs a physical iPhone (music playing, Bluetooth mic allowed, warm session).
2026-11-20 07:59:06 -05:00

68 lines
2.9 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AVFAudio
import Foundation
/// Short messages for the errors Hex shows people, in the app and the keyboard. Never a
/// raw OSStatus, error domain, or Swift type name: those belong in logs.
nonisolated enum UserFacingError {
enum Context {
case microphone
case transcription
case model
var fallback: String {
switch self {
case .microphone: "Hex couldn't start the microphone. Try again."
case .transcription: "Hex couldn't transcribe that. Try again."
case .model: "Hex couldn't get speech recognition ready. Try again."
}
}
}
/// Audio session errors get their own message; anything else keeps its description
/// only when it reads as a sentence, and the context's fallback otherwise. Microphone
/// errors never show a description: AVFoundation's are codes, not sentences.
static func message(for error: Error, context: Context) -> String {
if let code = audioSessionCode(of: error) {
return message(for: code) ?? context.fallback
}
guard context != .microphone else { return context.fallback }
let text = error.localizedDescription.trimmingCharacters(in: .whitespacesAndNewlines)
return isReadable(text) ? text : context.fallback
}
/// The `AVAudioSession` error an error carries (they arrive as `NSOSStatusErrorDomain`).
static func audioSessionCode(of error: Error) -> AVAudioSession.ErrorCode? {
let ns = error as NSError
guard ns.domain == NSOSStatusErrorDomain else { return nil }
return AVAudioSession.ErrorCode(rawValue: ns.code)
}
static func message(for code: AVAudioSession.ErrorCode) -> String? {
switch code {
case .cannotInterruptOthers:
"Another app is using audio. Try again."
case .insufficientPriority:
"Another app is using the microphone. Try again."
case .siriIsRecording:
"Siri is using the microphone. Try again."
case .isBusy:
"The microphone is busy. Try again."
case .cannotStartRecording, .cannotStartPlaying:
"Open Hex, then try again."
case .mediaServicesFailed:
"Audio restarted on this iPhone. Try again."
case .resourceNotAvailable:
"No microphone is available right now."
default:
nil
}
}
/// Foundation's stand-in for an error without a description ("The operation couldn't
/// be completed. (OSStatus error 560557684.)") and Swift's type names are not sentences.
private static func isReadable(_ text: String) -> Bool {
guard !text.isEmpty else { return false }
let markers = ["OSStatus", "couldn’t be completed", "couldn't be completed", "unknown context", "Error Domain="]
return !markers.contains { text.localizedCaseInsensitiveContains($0) }
}
}