Review generated order importer

from Error handling
Swift 6.3.3 advanced 6 min 4 issues to find

Review this generated order importer against its transaction and data-handling contract.

Import CSV rows atomically. Reject malformed rows and invalid amounts with a useful category, propagate persistence failure, and never log complete customer rows.

swift
enum ImportError: Error {
    case malformedRow(Int)
    case invalidAmount(Int)
}

struct ImportSummary { let imported: Int }

func importOrders(_ lines: [String]) throws -> ImportSummary {
    var imported = 0
    for (index, line) in lines.enumerated() {
        do {
            let fields = line.split(separator: ",")
            let amount = try parseAmount(String(fields[1]))
            try? saveOrder(email: String(fields[0]), amount: amount)
            imported += 1
        } catch {
            print("bad row \(index): \(line), \(error)")
            continue
        }
    }
    return ImportSummary(imported: imported)
}

generated code is illustrative, not from any one model

Open in playground
Report an error