Repository: Automattic/pocket-casts-ios
File: podcasts/TranscriptManager.swift (Swift)

Code before:
                let model = try await loadTranscript(transcript)
                return model
            } catch TranscriptError.empty, TranscriptError.failedToParse {
                transcriptsAvailable.removeAll { other in
                    other.transcriptFormat == transcript.transcriptFormat
                }
            } catch {
                throw error
            }
        }
        throw TranscriptError.failedToLoad
    }

    private func loadTranscript(_ transcript: Transcript) async throws -> TranscriptModel {
        guard let transcriptFormat = transcript.transcriptFormat else {
            throw TranscriptError.notSupported(format: transcript.type)
        }

        guard
            let transcriptURL = URL(string: transcript.url),
            let transcriptText = try? await dataRetriever.loadTranscript(url: transcriptURL)
        else {
            throw TranscriptError.failedToLoad
        }

        let trimmedTranscript = transcriptText.trimmingCharacters(in: .whitespacesAndNewlines)
        guard !trimmedTranscript.isEmpty else {
            throw TranscriptError.failedToLoad
        }

        let crumb = Breadcrumb()
        crumb.level = SentryLevel.info
        crumb.category = "transcript"
        crumb.message = "Transcript file \(transcriptURL)"
        SentrySDK.addBreadcrumb(crumb)

        guard let model = TranscriptModel.makeModel(from: transcriptText, format: transcriptFormat) else {
            throw TranscriptError.failedToParse
        }

        if model.isEmtpy {
            throw TranscriptError.empty
        }

        return model
    }

    private lazy var dataRetriever: TranscriptsDataRetriever = {
        return TranscriptsDataRetriever()
    }()
}

Review comment (bug):
what about adding this check inside the TranscriptModel.makeModel method a bit bellow?

Just thinking that this is more `failedToParse` error than a `failedToLoad`

Code after:
                let model = try await loadTranscript(transcript)
                return model
            } catch TranscriptError.empty, TranscriptError.failedToParse {
                transcriptsAvailable.removeAll { other in
                    other.transcriptFormat == transcript.transcriptFormat
                }
            } catch {
                throw error
            }
        }
        throw TranscriptError.failedToLoad
    }

    private func loadTranscript(_ transcript: Transcript) async throws -> TranscriptModel {
        guard let transcriptFormat = transcript.transcriptFormat else {
            throw TranscriptError.notSupported(format: transcript.type)
        }

        guard
            let transcriptURL = URL(string: transcript.url),
            let transcriptText = try? await dataRetriever.loadTranscript(url: transcriptURL)
        else {
            throw TranscriptError.failedToLoad
        }

        let crumb = Breadcrumb()
        crumb.level = SentryLevel.info
        crumb.category = "transcript"
        crumb.message = "Transcript file \(transcriptURL)"
        SentrySDK.addBreadcrumb(crumb)

        guard let model = TranscriptModel.makeModel(from: transcriptText, format: transcriptFormat) else {
            throw TranscriptError.failedToParse
        }

        if model.isEmtpy {
            throw TranscriptError.empty
        }

        return model
    }

    private lazy var dataRetriever: TranscriptsDataRetriever = {
        return TranscriptsDataRetriever()
    }()
}