Error Handling

FlatMessage provides comprehensive error handling through the Error enum. Understanding these errors helps you build robust serialization code.

Complete Error Reference

The following table lists all FlatMessage error types with their causes, typical scenarios, and recommended handling strategies:

Error TypeParametersWhen It OccursTypical CauseRecovery Strategy
InvalidHeaderLength(usize)Buffer sizeBuffer smaller than minimum header (8 bytes)Truncated data, wrong formatCheck data source, validate input
InvalidMagic-Magic number doesn't match "FLM\x01"Wrong file format, corruptionVerify file type, check data source
InvalidSize((u32, u32))(actual, expected)Size in header doesn't match buffer sizePartial read, corruptionRe-read data, validate source
InvalidOffsetSize-Invalid offset size encoding in headerCorruption, unsupported formatCheck format version, validate data
InvalidSizeToStoreMetaData((u32, u32))(actual, expected)Buffer too small for metadataIncomplete data, corruptionVerify complete transmission
InvalidHash((u32, u32))(actual, expected)CRC32 hash mismatchData corruption, tamperingRe-transmit data, check integrity
InvalidSizeToStoreFieldsTable((u32, u32))(actual, expected)Buffer too small for field tableTruncated dataEnsure complete data transfer
IncompatibleVersion(u8)Version numberStructure version incompatibilityVersion mismatchMigrate data, update code
FieldIsMissing(u32)Field hashField in data not in struct definitionSchema evolution, wrong structCheck struct version, migrate
InvalidFieldOffset((u32, u32))(actual, max)Field offset out of boundsCorruption, format errorValidate data integrity
FailToDeserialize(u32)Field hashFailed to deserialize specific fieldType mismatch, corruptionCheck field compatibility
NameNotStored-Name validation requested but not in dataMissing metadataDisable validation or add metadata
UnmatchedName-Structure name doesn't match stored nameWrong struct typeUse correct struct, check data
ChecksumNotStored-Checksum validation requested but not in dataMissing checksumDisable validation or add checksum
InvalidChecksum((u32, u32))(actual, expected)Checksum mismatchData corruptionRe-transmit, validate source
ExceedMaxSize((u32, u32))(actual, max)Serialized size exceeds maximumData too large, wrong limitIncrease limit, reduce data size

Error Categories

Data Format Errors

  • InvalidHeaderLength, InvalidMagic, InvalidSize, InvalidOffsetSize
  • Cause: Malformed or corrupted data format
  • Recovery: Validate data source, check file integrity

Data Integrity Errors

  • InvalidHash, InvalidChecksum
  • Cause: Data corruption during storage or transmission
  • Recovery: Re-transmit data, use error correction

Structure Compatibility Errors

  • IncompatibleVersion, FieldIsMissing, UnmatchedName
  • Cause: Schema evolution, version mismatches
  • Recovery: Migrate data, update compatibility rules

Configuration Errors

  • NameNotStored, ChecksumNotStored, ExceedMaxSize
  • Cause: Mismatched configuration between serialization and deserialization
  • Recovery: Align configurations, adjust limits

Field-Level Errors

  • InvalidFieldOffset, FailToDeserialize
  • Cause: Field-specific corruption or type mismatches
  • Recovery: Validate individual fields, check type compatibility

Example

The following example shows how to handle errors in a simple way when deserializing a struct.

#![allow(unused)]
fn main() {
use flat_message::*;

// consider that serialized_data is a buffer of the serialized data
let storage = Storage::from_buffer(serialized_data);

// Message is a struct that implements FlatMessage
match Message::deserialize_from(&storage) {
    Ok(restored_message) => {
        println!("Message serialized and deserialized successfully");
    }
    Err(e) => {
        panic!("Error deserializing message: {}", e);
    }
}
}