apache_avro/
error.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::{
19    schema::{Name, Schema, SchemaKind},
20    types::{Value, ValueKind},
21};
22use std::{error::Error as _, fmt};
23
24#[derive(thiserror::Error)]
25pub enum Error {
26    #[error("Bad Snappy CRC32; expected {expected:x} but got {actual:x}")]
27    SnappyCrc32 { expected: u32, actual: u32 },
28
29    #[error("Invalid u8 for bool: {0}")]
30    BoolValue(u8),
31
32    #[error("Not a fixed value, required for decimal with fixed schema: {0:?}")]
33    FixedValue(Value),
34
35    #[error("Not a bytes value, required for decimal with bytes schema: {0:?}")]
36    BytesValue(Value),
37
38    #[error("Not a string value, required for uuid: {0:?}")]
39    GetUuidFromStringValue(Value),
40
41    #[error("Two schemas with the same fullname were given: {0:?}")]
42    NameCollision(String),
43
44    #[error("Not a fixed or bytes type, required for decimal schema, got: {0:?}")]
45    ResolveDecimalSchema(SchemaKind),
46
47    #[error("Invalid utf-8 string")]
48    ConvertToUtf8(#[source] std::string::FromUtf8Error),
49
50    #[error("Invalid utf-8 string")]
51    ConvertToUtf8Error(#[source] std::str::Utf8Error),
52
53    /// Describes errors happened while validating Avro data.
54    #[error("Value does not match schema")]
55    Validation,
56
57    /// Describes errors happened while validating Avro data.
58    #[error("Value {value:?} does not match schema {schema:?}: Reason: {reason}")]
59    ValidationWithReason {
60        value: Value,
61        schema: Schema,
62        reason: String,
63    },
64
65    #[error("Unable to allocate {desired} bytes (maximum allowed: {maximum})")]
66    MemoryAllocation { desired: usize, maximum: usize },
67
68    /// Describe a specific error happening with decimal representation
69    #[error("Number of bytes requested for decimal sign extension {requested} is less than the number of bytes needed to decode {needed}")]
70    SignExtend { requested: usize, needed: usize },
71
72    #[error("Failed to read boolean bytes: {0}")]
73    ReadBoolean(#[source] std::io::Error),
74
75    #[error("Failed to read bytes: {0}")]
76    ReadBytes(#[source] std::io::Error),
77
78    #[error("Failed to read string: {0}")]
79    ReadString(#[source] std::io::Error),
80
81    #[error("Failed to read double: {0}")]
82    ReadDouble(#[source] std::io::Error),
83
84    #[error("Failed to read float: {0}")]
85    ReadFloat(#[source] std::io::Error),
86
87    #[error("Failed to read duration: {0}")]
88    ReadDuration(#[source] std::io::Error),
89
90    #[error("Failed to read fixed number of bytes '{1}': : {0}")]
91    ReadFixed(#[source] std::io::Error, usize),
92
93    #[error("Failed to convert &str to UUID: {0}")]
94    ConvertStrToUuid(#[source] uuid::Error),
95
96    #[error("Failed to convert Fixed bytes to UUID. It must be exactly 16 bytes, got {0}")]
97    ConvertFixedToUuid(usize),
98
99    #[error("Failed to convert Fixed bytes to UUID: {0}")]
100    ConvertSliceToUuid(#[source] uuid::Error),
101
102    #[error("Map key is not a string; key type is {0:?}")]
103    MapKeyType(ValueKind),
104
105    #[error("Union index {index} out of bounds: {num_variants}")]
106    GetUnionVariant { index: i64, num_variants: usize },
107
108    #[error("Enum symbol index out of bounds: {num_variants}")]
109    EnumSymbolIndex { index: usize, num_variants: usize },
110
111    #[error("Enum symbol not found {0}")]
112    GetEnumSymbol(String),
113
114    #[error("Unable to decode enum index")]
115    GetEnumUnknownIndexValue,
116
117    #[error("Scale {scale} is greater than precision {precision}")]
118    GetScaleAndPrecision { scale: usize, precision: usize },
119
120    #[error(
121        "Fixed type number of bytes {size} is not large enough to hold decimal values of precision {precision}"
122    )]
123    GetScaleWithFixedSize { size: usize, precision: usize },
124
125    #[error("Expected Value::Uuid, got: {0:?}")]
126    GetUuid(Value),
127
128    #[error("Expected Value::BigDecimal, got: {0:?}")]
129    GetBigDecimal(Value),
130
131    #[error("Fixed bytes of size 12 expected, got Fixed of size {0}")]
132    GetDecimalFixedBytes(usize),
133
134    #[error("Expected Value::Duration or Value::Fixed(12), got: {0:?}")]
135    ResolveDuration(Value),
136
137    #[error("Expected Value::Decimal, Value::Bytes or Value::Fixed, got: {0:?}")]
138    ResolveDecimal(Value),
139
140    #[error("Missing field in record: {0:?}")]
141    GetField(String),
142
143    #[error("Unable to convert to u8, got {0:?}")]
144    GetU8(Value),
145
146    #[error("Precision {precision} too small to hold decimal values with {num_bytes} bytes")]
147    ComparePrecisionAndSize { precision: usize, num_bytes: usize },
148
149    #[error("Cannot convert length to i32: {1}")]
150    ConvertLengthToI32(#[source] std::num::TryFromIntError, usize),
151
152    #[error("Expected Value::Date or Value::Int, got: {0:?}")]
153    GetDate(Value),
154
155    #[error("Expected Value::TimeMillis or Value::Int, got: {0:?}")]
156    GetTimeMillis(Value),
157
158    #[error("Expected Value::TimeMicros, Value::Long or Value::Int, got: {0:?}")]
159    GetTimeMicros(Value),
160
161    #[error("Expected Value::TimestampMillis, Value::Long or Value::Int, got: {0:?}")]
162    GetTimestampMillis(Value),
163
164    #[error("Expected Value::TimestampMicros, Value::Long or Value::Int, got: {0:?}")]
165    GetTimestampMicros(Value),
166
167    #[error("Expected Value::TimestampNanos, Value::Long or Value::Int, got: {0:?}")]
168    GetTimestampNanos(Value),
169
170    #[error("Expected Value::LocalTimestampMillis, Value::Long or Value::Int, got: {0:?}")]
171    GetLocalTimestampMillis(Value),
172
173    #[error("Expected Value::LocalTimestampMicros, Value::Long or Value::Int, got: {0:?}")]
174    GetLocalTimestampMicros(Value),
175
176    #[error("Expected Value::LocalTimestampNanos, Value::Long or Value::Int, got: {0:?}")]
177    GetLocalTimestampNanos(Value),
178
179    #[error("Expected Value::Null, got: {0:?}")]
180    GetNull(Value),
181
182    #[error("Expected Value::Boolean, got: {0:?}")]
183    GetBoolean(Value),
184
185    #[error("Expected Value::Int, got: {0:?}")]
186    GetInt(Value),
187
188    #[error("Expected Value::Long or Value::Int, got: {0:?}")]
189    GetLong(Value),
190
191    #[error(r#"Expected Value::Double, Value::Float, Value::Int, Value::Long or Value::String ("NaN", "INF", "Infinity", "-INF" or "-Infinity"), got: {0:?}"#)]
192    GetDouble(Value),
193
194    #[error(r#"Expected Value::Float, Value::Double, Value::Int, Value::Long or Value::String ("NaN", "INF", "Infinity", "-INF" or "-Infinity"), got: {0:?}"#)]
195    GetFloat(Value),
196
197    #[error("Expected Value::Bytes, got: {0:?}")]
198    GetBytes(Value),
199
200    #[error("Expected Value::String, Value::Bytes or Value::Fixed, got: {0:?}")]
201    GetString(Value),
202
203    #[error("Expected Value::Enum, got: {0:?}")]
204    GetEnum(Value),
205
206    #[error("Fixed size mismatch, expected: {size}, got: {n}")]
207    CompareFixedSizes { size: usize, n: usize },
208
209    #[error("String expected for fixed, got: {0:?}")]
210    GetStringForFixed(Value),
211
212    #[error("Enum default {symbol:?} is not among allowed symbols {symbols:?}")]
213    GetEnumDefault {
214        symbol: String,
215        symbols: Vec<String>,
216    },
217
218    #[error("Enum value index {index} is out of bounds {nsymbols}")]
219    GetEnumValue { index: usize, nsymbols: usize },
220
221    #[error("Key {0} not found in decimal metadata JSON")]
222    GetDecimalMetadataFromJson(&'static str),
223
224    #[error("Could not find matching type in union")]
225    FindUnionVariant,
226
227    #[error("Union type should not be empty")]
228    EmptyUnion,
229
230    #[error("Array({expected:?}) expected, got {other:?}")]
231    GetArray { expected: SchemaKind, other: Value },
232
233    #[error("Map({expected:?}) expected, got {other:?}")]
234    GetMap { expected: SchemaKind, other: Value },
235
236    #[error("Record with fields {expected:?} expected, got {other:?}")]
237    GetRecord {
238        expected: Vec<(String, SchemaKind)>,
239        other: Value,
240    },
241
242    #[error("No `name` field")]
243    GetNameField,
244
245    #[error("No `name` in record field")]
246    GetNameFieldFromRecord,
247
248    #[error("Unions may not directly contain a union")]
249    GetNestedUnion,
250
251    #[error("Unions cannot contain duplicate types")]
252    GetUnionDuplicate,
253
254    #[error("One union type {0:?} must match the `default`'s value type {1:?}")]
255    GetDefaultUnion(SchemaKind, ValueKind),
256
257    #[error("`default`'s value type of field {0:?} in {1:?} must be {2:?}")]
258    GetDefaultRecordField(String, String, String),
259
260    #[error("JSON value {0} claims to be u64 but cannot be converted")]
261    GetU64FromJson(serde_json::Number),
262
263    #[error("JSON value {0} claims to be i64 but cannot be converted")]
264    GetI64FromJson(serde_json::Number),
265
266    #[error("Cannot convert u64 to usize: {1}")]
267    ConvertU64ToUsize(#[source] std::num::TryFromIntError, u64),
268
269    #[error("Cannot convert u32 to usize: {1}")]
270    ConvertU32ToUsize(#[source] std::num::TryFromIntError, u32),
271
272    #[error("Cannot convert i64 to usize: {1}")]
273    ConvertI64ToUsize(#[source] std::num::TryFromIntError, i64),
274
275    #[error("Cannot convert i32 to usize: {1}")]
276    ConvertI32ToUsize(#[source] std::num::TryFromIntError, i32),
277
278    #[error("Invalid JSON value for decimal precision/scale integer: {0}")]
279    GetPrecisionOrScaleFromJson(serde_json::Number),
280
281    #[error("Failed to parse schema from JSON")]
282    ParseSchemaJson(#[source] serde_json::Error),
283
284    #[error("Failed to read schema")]
285    ReadSchemaFromReader(#[source] std::io::Error),
286
287    #[error("Must be a JSON string, object or array")]
288    ParseSchemaFromValidJson,
289
290    #[error("Unknown primitive type: {0}")]
291    ParsePrimitive(String),
292
293    #[error("invalid JSON for {key:?}: {value:?}")]
294    GetDecimalMetadataValueFromJson {
295        key: String,
296        value: serde_json::Value,
297    },
298
299    #[error("The decimal precision ({precision}) must be bigger or equal to the scale ({scale})")]
300    DecimalPrecisionLessThanScale { precision: usize, scale: usize },
301
302    #[error("The decimal precision ({precision}) must be a positive number")]
303    DecimalPrecisionMuBePositive { precision: usize },
304
305    #[error("Unreadable big decimal sign")]
306    BigDecimalSign,
307
308    #[error("Unreadable length for big decimal inner bytes: {0}")]
309    BigDecimalLen(#[source] Box<Error>),
310
311    #[error("Unreadable big decimal scale")]
312    BigDecimalScale,
313
314    #[error("Unexpected `type` {0} variant for `logicalType`")]
315    GetLogicalTypeVariant(serde_json::Value),
316
317    #[error("No `type` field found for `logicalType`")]
318    GetLogicalTypeField,
319
320    #[error("logicalType must be a string, but is {0:?}")]
321    GetLogicalTypeFieldType(serde_json::Value),
322
323    #[error("Unknown complex type: {0}")]
324    GetComplexType(serde_json::Value),
325
326    #[error("No `type` in complex type")]
327    GetComplexTypeField,
328
329    #[error("No `fields` in record")]
330    GetRecordFieldsJson,
331
332    #[error("No `symbols` field in enum")]
333    GetEnumSymbolsField,
334
335    #[error("Unable to parse `symbols` in enum")]
336    GetEnumSymbols,
337
338    #[error("Invalid enum symbol name {0}")]
339    EnumSymbolName(String),
340
341    #[error("Invalid field name {0}")]
342    FieldName(String),
343
344    #[error("Duplicate field name {0}")]
345    FieldNameDuplicate(String),
346
347    #[error("Invalid schema name {0}. It must match the regex '{1}'")]
348    InvalidSchemaName(String, &'static str),
349
350    #[error("Invalid namespace {0}. It must match the regex '{1}'")]
351    InvalidNamespace(String, &'static str),
352
353    #[error("Invalid schema: There is no type called '{0}', if you meant to define a non-primitive schema, it should be defined inside `type` attribute. Please review the specification")]
354    InvalidSchemaRecord(String),
355
356    #[error("Duplicate enum symbol {0}")]
357    EnumSymbolDuplicate(String),
358
359    #[error("Default value for enum must be a string! Got: {0}")]
360    EnumDefaultWrongType(serde_json::Value),
361
362    #[error("No `items` in array")]
363    GetArrayItemsField,
364
365    #[error("No `values` in map")]
366    GetMapValuesField,
367
368    #[error("Fixed schema `size` value must be a positive integer: {0}")]
369    GetFixedSizeFieldPositive(serde_json::Value),
370
371    #[error("Fixed schema has no `size`")]
372    GetFixedSizeField,
373
374    #[error("Fixed schema's default value length ({0}) does not match its size ({1})")]
375    FixedDefaultLenSizeMismatch(usize, u64),
376
377    #[error("Failed to compress with flate: {0}")]
378    DeflateCompress(#[source] std::io::Error),
379
380    // no longer possible after migration from libflate to miniz_oxide
381    #[deprecated(since = "0.19.0", note = "This error can no longer occur")]
382    #[error("Failed to finish flate compressor: {0}")]
383    DeflateCompressFinish(#[source] std::io::Error),
384
385    #[error("Failed to decompress with flate: {0}")]
386    DeflateDecompress(#[source] std::io::Error),
387
388    #[cfg(feature = "snappy")]
389    #[error("Failed to compress with snappy: {0}")]
390    SnappyCompress(#[source] snap::Error),
391
392    #[cfg(feature = "snappy")]
393    #[error("Failed to get snappy decompression length: {0}")]
394    GetSnappyDecompressLen(#[source] snap::Error),
395
396    #[cfg(feature = "snappy")]
397    #[error("Failed to decompress with snappy: {0}")]
398    SnappyDecompress(#[source] snap::Error),
399
400    #[error("Failed to compress with zstd: {0}")]
401    ZstdCompress(#[source] std::io::Error),
402
403    #[error("Failed to decompress with zstd: {0}")]
404    ZstdDecompress(#[source] std::io::Error),
405
406    #[error("Failed to read header: {0}")]
407    ReadHeader(#[source] std::io::Error),
408
409    #[error("wrong magic in header")]
410    HeaderMagic,
411
412    #[error("Message Header mismatch. Expected: {0:?}. Actual: {1:?}")]
413    SingleObjectHeaderMismatch([u8; 10], [u8; 10]),
414
415    #[error("Failed to get JSON from avro.schema key in map")]
416    GetAvroSchemaFromMap,
417
418    #[error("no metadata in header")]
419    GetHeaderMetadata,
420
421    #[error("Failed to read marker bytes: {0}")]
422    ReadMarker(#[source] std::io::Error),
423
424    #[error("Failed to read block marker bytes: {0}")]
425    ReadBlockMarker(#[source] std::io::Error),
426
427    #[error("Read into buffer failed: {0}")]
428    ReadIntoBuf(#[source] std::io::Error),
429
430    #[error("block marker does not match header marker")]
431    GetBlockMarker,
432
433    #[error("Overflow when decoding integer value")]
434    IntegerOverflow,
435
436    #[error("Failed to read bytes for decoding variable length integer: {0}")]
437    ReadVariableIntegerBytes(#[source] std::io::Error),
438
439    #[error("Decoded integer out of range for i32: {1}: {0}")]
440    ZagI32(#[source] std::num::TryFromIntError, i64),
441
442    #[error("unable to read block")]
443    ReadBlock,
444
445    #[error("Failed to serialize value into Avro value: {0}")]
446    SerializeValue(String),
447
448    #[error("Failed to serialize value of type {value_type} using schema {schema:?}: {value}")]
449    SerializeValueWithSchema {
450        value_type: &'static str,
451        value: String,
452        schema: Schema,
453    },
454
455    #[error("Failed to serialize field '{field_name}' for record {record_schema:?}: {error}")]
456    SerializeRecordFieldWithSchema {
457        field_name: &'static str,
458        record_schema: Schema,
459        error: Box<Error>,
460    },
461
462    #[error("Failed to deserialize Avro value into value: {0}")]
463    DeserializeValue(String),
464
465    #[error("Failed to write buffer bytes during flush: {0}")]
466    WriteBytes(#[source] std::io::Error),
467
468    #[error("Failed to flush inner writer during flush: {0}")]
469    FlushWriter(#[source] std::io::Error),
470
471    #[error("Failed to write marker: {0}")]
472    WriteMarker(#[source] std::io::Error),
473
474    #[error("Failed to convert JSON to string: {0}")]
475    ConvertJsonToString(#[source] serde_json::Error),
476
477    /// Error while converting float to json value
478    #[error("failed to convert avro float to json: {0}")]
479    ConvertF64ToJson(f64),
480
481    /// Error while resolving Schema::Ref
482    #[error("Unresolved schema reference: {0}")]
483    SchemaResolutionError(Name),
484
485    #[error("The file metadata is already flushed.")]
486    FileHeaderAlreadyWritten,
487
488    #[error("Metadata keys starting with 'avro.' are reserved for internal usage: {0}.")]
489    InvalidMetadataKey(String),
490
491    /// Error when two named schema have the same fully qualified name
492    #[error("Two named schema defined for same fullname: {0}.")]
493    AmbiguousSchemaDefinition(Name),
494
495    #[error("Signed decimal bytes length {0} not equal to fixed schema size {1}.")]
496    EncodeDecimalAsFixedError(usize, usize),
497
498    #[error("There is no entry for '{0}' in the lookup table: {1}.")]
499    NoEntryInLookupTable(String, String),
500
501    #[error("Can only encode value type {value_kind:?} as one of {supported_schema:?}")]
502    EncodeValueAsSchemaError {
503        value_kind: ValueKind,
504        supported_schema: Vec<SchemaKind>,
505    },
506    #[error(
507        "Internal buffer not drained properly. Re-initialize the single object writer struct!"
508    )]
509    IllegalSingleObjectWriterState,
510
511    #[error("Codec '{0}' is not supported/enabled")]
512    CodecNotSupported(String),
513
514    #[error("Invalid Avro data! Cannot read codec type from value that is not Value::Bytes.")]
515    BadCodecMetadata,
516}
517
518#[derive(thiserror::Error, PartialEq)]
519pub enum CompatibilityError {
520    #[error("Incompatible schema types! Writer schema is '{writer_schema_type}', but reader schema is '{reader_schema_type}'")]
521    WrongType {
522        writer_schema_type: String,
523        reader_schema_type: String,
524    },
525
526    #[error("Incompatible schema types! The {schema_type} should have been {expected_type:?}")]
527    TypeExpected {
528        schema_type: String,
529        expected_type: Vec<SchemaKind>,
530    },
531
532    #[error("Incompatible schemata! Field '{0}' in reader schema does not match the type in the writer schema")]
533    FieldTypeMismatch(String, #[source] Box<CompatibilityError>),
534
535    #[error("Incompatible schemata! Field '{0}' in reader schema must have a default value")]
536    MissingDefaultValue(String),
537
538    #[error("Incompatible schemata! Reader's symbols must contain all writer's symbols")]
539    MissingSymbols,
540
541    #[error("Incompatible schemata! All elements in union must match for both schemas")]
542    MissingUnionElements,
543
544    #[error("Incompatible schemata! Name and size don't match for fixed")]
545    FixedMismatch,
546
547    #[error("Incompatible schemata! The name must be the same for both schemas. Writer's name {writer_name} and reader's name {reader_name}")]
548    NameMismatch {
549        writer_name: String,
550        reader_name: String,
551    },
552
553    #[error(
554        "Incompatible schemata! Unknown type for '{0}'. Make sure that the type is a valid one"
555    )]
556    Inconclusive(String),
557}
558
559impl serde::ser::Error for Error {
560    fn custom<T: fmt::Display>(msg: T) -> Self {
561        Error::SerializeValue(msg.to_string())
562    }
563}
564
565impl serde::de::Error for Error {
566    fn custom<T: fmt::Display>(msg: T) -> Self {
567        Error::DeserializeValue(msg.to_string())
568    }
569}
570
571impl fmt::Debug for Error {
572    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
573        let mut msg = self.to_string();
574        if let Some(e) = self.source() {
575            msg.extend([": ", &e.to_string()]);
576        }
577        write!(f, "{}", msg)
578    }
579}
580
581impl fmt::Debug for CompatibilityError {
582    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
583        let mut msg = self.to_string();
584        if let Some(e) = self.source() {
585            msg.extend([": ", &e.to_string()]);
586        }
587        write!(f, "{}", msg)
588    }
589}