|
| 1 | +# Phase 4: Error Handling and Versioning |
| 2 | + |
| 3 | +**Status**: 🔄 IN PROGRESS |
| 4 | +**Priority**: MEDIUM |
| 5 | +**Estimated Time**: 1-2 days |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Goals |
| 10 | + |
| 11 | +1. Add structured error handling instead of generic runtime_error |
| 12 | +2. Implement versioning for serialized data |
| 13 | +3. Add backward compatibility support |
| 14 | +4. Improve error messages |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Error Handling Strategy |
| 19 | + |
| 20 | +### Current Issues |
| 21 | + |
| 22 | +```cpp |
| 23 | +// Too generic - hard to handle programmatically |
| 24 | +throw std::runtime_error("Given index is already included."); |
| 25 | +throw std::runtime_error("Given index is not found."); |
| 26 | +throw std::runtime_error("invalid shape."); |
| 27 | +``` |
| 28 | +
|
| 29 | +### Proposed Solution |
| 30 | +
|
| 31 | +Add specific exception types: |
| 32 | +
|
| 33 | +```cpp |
| 34 | +class PRTreeException : public std::runtime_error { |
| 35 | +public: |
| 36 | + enum class ErrorCode { |
| 37 | + INDEX_NOT_FOUND, |
| 38 | + INDEX_ALREADY_EXISTS, |
| 39 | + INVALID_SHAPE, |
| 40 | + INVALID_BOUNDS, |
| 41 | + INVALID_DIMENSION, |
| 42 | + FILE_NOT_FOUND, |
| 43 | + SERIALIZATION_ERROR, |
| 44 | + VERSION_MISMATCH |
| 45 | + }; |
| 46 | +
|
| 47 | + PRTreeException(ErrorCode code, const std::string& message) |
| 48 | + : std::runtime_error(message), code_(code) {} |
| 49 | +
|
| 50 | + ErrorCode code() const noexcept { return code_; } |
| 51 | +
|
| 52 | +private: |
| 53 | + ErrorCode code_; |
| 54 | +}; |
| 55 | +``` |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## Versioning Strategy |
| 60 | + |
| 61 | +### File Format Versioning |
| 62 | + |
| 63 | +Add version header to saved files: |
| 64 | + |
| 65 | +```cpp |
| 66 | +struct PRTreeHeader { |
| 67 | + uint32_t magic = 0x50525452; // "PRTR" |
| 68 | + uint16_t major_version = 1; |
| 69 | + uint16_t minor_version = 0; |
| 70 | + uint32_t crc32 = 0; // Optional integrity check |
| 71 | +}; |
| 72 | + |
| 73 | +void save(const std::string& fname) const { |
| 74 | + std::lock_guard<std::mutex> lock(tree_mutex_); |
| 75 | + std::ofstream ofs(fname, std::ios::binary); |
| 76 | + |
| 77 | + // Write header |
| 78 | + PRTreeHeader header; |
| 79 | + header.major_version = PRTREE_VERSION_MAJOR; |
| 80 | + header.minor_version = PRTREE_VERSION_MINOR; |
| 81 | + ofs.write(reinterpret_cast<const char*>(&header), sizeof(header)); |
| 82 | + |
| 83 | + // Write data |
| 84 | + cereal::PortableBinaryOutputArchive o_archive(ofs); |
| 85 | + o_archive(...); |
| 86 | +} |
| 87 | +``` |
| 88 | +
|
| 89 | +### Backward Compatibility |
| 90 | +
|
| 91 | +```cpp |
| 92 | +void load(const std::string& fname) { |
| 93 | + std::lock_guard<std::mutex> lock(tree_mutex_); |
| 94 | + std::ifstream ifs(fname, std::ios::binary); |
| 95 | +
|
| 96 | + // Read header |
| 97 | + PRTreeHeader header; |
| 98 | + ifs.read(reinterpret_cast<char*>(&header), sizeof(header)); |
| 99 | +
|
| 100 | + if (header.magic != 0x50525452) { |
| 101 | + // Legacy format (no header) |
| 102 | + ifs.seekg(0); |
| 103 | + load_legacy(ifs); |
| 104 | + return; |
| 105 | + } |
| 106 | +
|
| 107 | + if (header.major_version > PRTREE_VERSION_MAJOR) { |
| 108 | + throw PRTreeException( |
| 109 | + ErrorCode::VERSION_MISMATCH, |
| 110 | + "File version too new" |
| 111 | + ); |
| 112 | + } |
| 113 | +
|
| 114 | + // Load based on version |
| 115 | + load_v1(ifs, header); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## Implementation Plan |
| 122 | + |
| 123 | +1. ✅ Document error handling strategy |
| 124 | +2. Define PRTreeException class |
| 125 | +3. Replace runtime_error with specific exceptions |
| 126 | +4. Add version constants |
| 127 | +5. Implement versioned save/load |
| 128 | +6. Add backward compatibility for existing files |
| 129 | +7. Update error messages to be more descriptive |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +## Benefits |
| 134 | + |
| 135 | +- **Better error handling**: Users can catch specific exceptions |
| 136 | +- **Forward compatibility**: New versions can read old files |
| 137 | +- **Backward compatibility**: Detect and handle version mismatches |
| 138 | +- **Debugging**: Better error messages |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +## Success Criteria |
| 143 | + |
| 144 | +- ✅ All exceptions use specific error codes |
| 145 | +- ✅ Version header in all saved files |
| 146 | +- ✅ Can load files from previous versions |
| 147 | +- ✅ Clear error messages for all failure cases |
0 commit comments