1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![allow(missing_docs)]

use serde::{de, ser};
use std::fmt::Debug;
use std::fmt::Display;
use std::io::{Error, ErrorKind};

#[inline(always)]
pub(crate) fn io_err(msg: &str) -> Error {
    Error::new(ErrorKind::Other, msg)
}

// Required impls for Serde
impl ser::Error for AvrowErr {
    fn custom<T: Display>(msg: T) -> Self {
        Self::Message(msg.to_string())
    }
}

impl de::Error for AvrowErr {
    fn custom<T: Display>(msg: T) -> Self {
        Self::Message(msg.to_string())
    }
}

pub type AvrowResult<T> = Result<T, AvrowErr>;

/// Errors returned from avrow
#[derive(thiserror::Error, Debug)]
pub enum AvrowErr {
    // Encode errors
    #[error("Write failed")]
    EncodeFailed(#[source] std::io::Error),
    #[error("Encoding failed. Value does not match schema")]
    SchemaDataMismatch,
    #[error("Expected magic header: `Obj\n`")]
    InvalidDataFile,
    #[error("Sync marker does not match as expected")]
    SyncMarkerMismatch,
    #[error("Named schema not found in union")]
    SchemaNotFoundInUnion,
    #[error("Invalid field value: {0}")]
    InvalidFieldValue(String),
    #[error("Writer seek failed, not a valid avro data file")]
    WriterSeekFailed,
    #[error("Unions must not contain immediate union values")]
    NoImmediateUnion,
    #[error("Failed building the Writer")]
    WriterBuildFailed,
    #[error("Json must be an object for record")]
    ExpectedJsonObject,

    // Decode errors
    #[error("Read failed")]
    DecodeFailed(#[source] std::io::Error),
    #[error("failed reading `avro.schema` metadata from header")]
    HeaderDecodeFailed,
    #[error("Unsupported codec {0}, did you enable the feature?")]
    UnsupportedCodec(String),
    #[error("Named schema was not found in schema registry")]
    NamedSchemaNotFound,
    #[error("Schema resolution failed. reader's schema {0} != writer's schema {1}")]
    SchemaResolutionFailed(String, String),
    #[error("Index read for enum is out of range as per schema. got: {0} symbols: {1}")]
    InvalidEnumSymbolIdx(usize, String),
    #[error("Field not found in record")]
    FieldNotFound,
    #[error("Writer schema not found in reader's schema")]
    WriterNotInReader,
    #[error("Reader's union schema does not match with writer's union schema")]
    UnionSchemaMismatch,
    #[error("Map's value schema do not match")]
    MapSchemaMismatch,
    #[error("Fixed schema names do not match")]
    FixedSchemaNameMismatch,
    #[error("Could not find symbol at index {idx} in reader schema")]
    EnumSymbolNotFound { idx: usize },
    #[error("Reader's enum name does not match writer's enum name")]
    EnumNameMismatch,
    #[error("Readers' record name does not match writer's record name")]
    RecordNameMismatch,
    #[error("Array items schema does not match")]
    ArrayItemsMismatch,
    #[error("Snappy decoder failed to get length of decompressed buffer")]
    SnappyDecompressLenFailed,
    #[error("End of file reached")]
    Eof,

    // Schema parse errors
    #[error("Failed to parse avro schema")]
    SchemaParseErr(#[source] std::io::Error),
    #[error("Unknown schema, expecting a required `type` field in schema")]
    SchemaParseFailed,
    #[error("Expecting fields key as a json array, found: {0}")]
    SchemaFieldParseErr(String),
    #[error("Expected: {0}, found: {1}")]
    SchemaDataValidationFailed(String, String),
    #[error("Schema has a field not found in the value")]
    RecordFieldMissing,
    #[error("Record schema does not a have a required field named `name`")]
    RecordNameNotFound,
    #[error("Record schema does not a have a required field named `type`")]
    RecordTypeNotFound,
    #[error("Expected record field to be a json array")]
    ExpectedFieldsJsonArray,
    #[error("Record's field json schema must be an object")]
    InvalidRecordFieldType,
    #[error("{0}")]
    ParseFieldOrderErr(String),
    #[error("Could not parse name from json value")]
    NameParseFailed,
    #[error("Parsing canonical form failed")]
    ParsingCanonicalForm,
    #[error("Duplicate definition of named schema")]
    DuplicateSchema,
    #[error("Invalid default value for union. Must be the first entry from union definition")]
    FailedDefaultUnion,
    #[error("Invalid default value for given schema")]
    DefaultValueParse,
    #[error("Unknown field ordering value.")]
    UnknownFieldOrdering,
    #[error("Field ordering value must be a string")]
    InvalidFieldOrdering,
    #[error("Failed to parse symbol from enum's symbols field")]
    EnumSymbolParseErr,
    #[error("Enum schema must contain required `symbols` field")]
    EnumSymbolsMissing,
    #[error("Enum value symbol not present in enum schema `symbols` field")]
    EnumSymbolNotPresent,
    #[error("Fixed schema `size` field must be a number")]
    FixedSizeNotNumber,
    #[error("Fixed schema `size` field missing")]
    FixedSizeNotFound,
    #[error("Unions cannot have multiple schemas of same type or immediate unions")]
    DuplicateSchemaInUnion,
    #[error("Expected the avro schema to be as one of json string, object or an array")]
    UnknownSchema,
    #[error("Expected record field to be a json object, found {0}")]
    InvalidSchema(String),
    #[error("{0}")]
    InvalidDefaultValue(String),
    #[error("Invalid type for {0}")]
    InvalidType(String),
    #[error("Enum schema parsing failed, found: {0}")]
    EnumParseErr(String),
    #[error("Primitve schema must be a string")]
    InvalidPrimitiveSchema,

    // Validation errors
    #[error("Mismatch in fixed bytes length: {found}, {expected}")]
    FixedValueLenMismatch { found: usize, expected: usize },
    #[error("namespaces must either be empty or follow the grammer <name>[(<dot><name>)*")]
    InvalidNamespace,
    #[error("Field name must be [A-Za-z_] and subsequently contain only [A-Za-z0-9_]")]
    InvalidName,
    #[error("Array value is empty")]
    EmptyArray,
    #[error("Map value is empty")]
    EmptyMap,
    #[error("Crc generation failed")]
    CRCGenFailed,
    #[error("Snappy Crc mismatch")]
    CRCMismatch { found: u32, expected: u32 },
    #[error("Named schema was not found for given value")]
    NamedSchemaNotFoundForValue,
    #[error("Value schema not found in union")]
    NotFoundInUnion,

    // Serde specific errors
    #[error("Serde error: {0}")]
    Message(String),
    #[error("Syntax error occured")]
    Syntax,
    #[error("Expected a string value")]
    ExpectedString,
    #[error("Unsupported type")]
    Unsupported,
    #[error("Unexpected avro value: {value}")]
    UnexpectedAvroValue { value: String },

    // Value errors
    #[error("Expected value not found in variant instance")]
    ExpectedVariantNotFound,
}