Error Handling

FileStorageError

Enum representing errors from file storage operations:

public enum FileStorageError: Error, LocalizedError {
    case directoryCreationFailed(Error)
    case fileWriteFailed(Error)
    case fileReadFailed(Error)
    case fileNotFound(String)
    case decodingFailed(Error)
    case encodingFailed(Error)
}

Error Cases

CaseWrapped TypeDescription
directoryCreationFailedErrorFailed to create records directory structure
fileWriteFailedErrorFailed to write JSON file to disk
fileReadFailedErrorFailed to read existing file
fileNotFoundString (path)Requested file does not exist
decodingFailedErrorJSON parsing failed
encodingFailedErrorJSON serialization failed

Localized Descriptions

All cases conform to LocalizedError with errorDescription:

case .directoryCreationFailed(let error):
    return "Failed to create directory: \(error.localizedDescription)"
case .fileWriteFailed(let error):
    return "Failed to write file: \(error.localizedDescription)"
case .fileReadFailed(let error):
    return "Failed to read file: \(error.localizedDescription)"
case .fileNotFound(let path):
    return "File not found: \(path)"
case .decodingFailed(let error):
    return "Failed to decode data: \(error.localizedDescription)"
case .encodingFailed(let error):
    return "Failed to encode data: \(error.localizedDescription)"

Error Handling Pattern

Errors bubble up through the actor boundary and should be caught at the UI layer:

do {
    let records = try await timeTrackingService.getTodayRecords()
} catch let error as FileStorageError {
    // Handle specific storage errors
} catch {
    // Handle unexpected errors
}

Related