File size: 2,317 Bytes
64f7370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Foundation

/// All errors thrown by SpeechKit. Every public API throws only this type.
public enum SpeechError: Error, LocalizedError, Equatable {
    // Asset / model loading
    case assetBundleNotFound(String)
    case assetMissing(String)
    case assetCorrupted(name: String, expectedSHA256: String, actualSHA256: String)
    case assetSchemaUnsupported(found: Int, supported: ClosedRange<Int>)
    case modelCompilationFailed(String)
    case modelLoadFailed(String)

    // Audio input
    case audioTooShort(minimumSeconds: Double)
    case audioFormatUnsupported(String)
    case resamplingFailed(String)

    // Inference
    case inferenceFailed(stage: String, underlying: String)
    case promptTooLong(tokens: Int, limit: Int)
    case cancelled

    public var errorDescription: String? {
        switch self {
        case .assetBundleNotFound(let path):
            return "Model asset bundle not found at \(path)"
        case .assetMissing(let name):
            return "Required asset missing from bundle: \(name)"
        case .assetCorrupted(let name, let expected, let actual):
            return "Asset \(name) failed integrity check (expected \(expected.prefix(12))..., got \(actual.prefix(12))...)"
        case .assetSchemaUnsupported(let found, let supported):
            return "Asset schema v\(found) is outside supported range v\(supported.lowerBound)-v\(supported.upperBound); update SpeechKit or the model bundle"
        case .modelCompilationFailed(let detail):
            return "Core ML model compilation failed: \(detail)"
        case .modelLoadFailed(let detail):
            return "Model load failed: \(detail)"
        case .audioTooShort(let min):
            return "Audio shorter than the \(min)s minimum"
        case .audioFormatUnsupported(let detail):
            return "Unsupported audio format: \(detail)"
        case .resamplingFailed(let detail):
            return "Audio resampling failed: \(detail)"
        case .inferenceFailed(let stage, let underlying):
            return "Inference failed at \(stage): \(underlying)"
        case .promptTooLong(let tokens, let limit):
            return "Prompt of \(tokens) tokens exceeds the decoder limit of \(limit)"
        case .cancelled:
            return "Operation was cancelled"
        }
    }
}