Skip to main content

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, RecordSchema, Schema, SchemaKind, UnionSchema},
20    types::{Value, ValueKind},
21};
22use std::num::NonZero;
23use std::{error::Error as _, fmt};
24
25/// Errors encountered by Avro.
26///
27/// To inspect the details of the error use [`details`](Self::details) or [`into_details`](Self::into_details)
28/// to get a [`Details`] which contains more precise error information.
29///
30/// See [`Details`] for all possible errors.
31#[derive(thiserror::Error, Debug)]
32#[repr(transparent)]
33#[error(transparent)]
34pub struct Error {
35    details: Box<Details>,
36}
37
38impl Error {
39    pub fn new(details: Details) -> Self {
40        Self {
41            details: Box::new(details),
42        }
43    }
44
45    pub fn details(&self) -> &Details {
46        &self.details
47    }
48
49    pub fn into_details(self) -> Details {
50        *self.details
51    }
52}
53
54impl From<Details> for Error {
55    fn from(details: Details) -> Self {
56        Self::new(details)
57    }
58}
59
60impl serde::ser::Error for Error {
61    fn custom<T: fmt::Display>(msg: T) -> Self {
62        Self::new(<Details as serde::ser::Error>::custom(msg))
63    }
64}
65
66impl serde::de::Error for Error {
67    fn custom<T: fmt::Display>(msg: T) -> Self {
68        Self::new(<Details as serde::de::Error>::custom(msg))
69    }
70}
71
72#[derive(thiserror::Error)]
73pub enum Details {
74    #[error("Bad Snappy CRC32; expected {expected:x} but got {actual:x}")]
75    SnappyCrc32 { expected: u32, actual: u32 },
76
77    #[error("Invalid u8 for bool: {0}")]
78    BoolValue(u8),
79
80    #[error("Not a fixed value, required for decimal with fixed schema: {0:?}")]
81    FixedValue(Value),
82
83    #[error("Not a bytes value, required for decimal with bytes schema: {0:?}")]
84    BytesValue(Value),
85
86    #[error("Not a string value, required for uuid: {0:?}")]
87    GetUuidFromStringValue(Value),
88
89    #[error("Two schemas with the same fullname were given: {0:?}")]
90    NameCollision(String),
91
92    #[error("Not a fixed or bytes type, required for decimal schema, got: {0:?}")]
93    ResolveDecimalSchema(SchemaKind),
94
95    #[error("Decimal bytes cannot be zero length")]
96    DecimalIsZeroLength,
97
98    #[error("Invalid utf-8 string")]
99    ConvertToUtf8(#[source] std::string::FromUtf8Error),
100
101    #[error("Invalid utf-8 string")]
102    ConvertToUtf8Error(#[source] std::str::Utf8Error),
103
104    /// Describes errors happened while validating Avro data.
105    #[error("Value does not match schema")]
106    Validation,
107
108    /// Describes errors happened while validating Avro data.
109    #[error("Value {value:?} does not match schema {schema:?}: Reason: {reason}")]
110    ValidationWithReason {
111        value: Value,
112        schema: Schema,
113        reason: String,
114    },
115
116    #[error(
117        "{} (maximum allowed: {maximum}). Change the limit using `apache_avro::util::max_allocation_bytes`",
118        if let Some(desired) = desired {
119            format!("Unable to allocate {desired} bytes")
120        } else {
121            "Allocation limit reached with unknown amount of bytes remaining".to_string()
122        },
123    )]
124    MemoryAllocation {
125        desired: Option<usize>,
126        maximum: usize,
127    },
128
129    /// Describe a specific error happening with decimal representation
130    #[error(
131        "Number of bytes requested for decimal sign extension {requested} is less than the number of bytes needed to decode {needed}"
132    )]
133    SignExtend { requested: usize, needed: usize },
134
135    #[error("Failed to read boolean bytes: {0}")]
136    ReadBoolean(#[source] std::io::Error),
137
138    #[error("Failed to read bytes: {0}")]
139    ReadBytes(#[source] std::io::Error),
140
141    #[error("Failed to read string: {0}")]
142    ReadString(#[source] std::io::Error),
143
144    #[error("Failed to read double: {0}")]
145    ReadDouble(#[source] std::io::Error),
146
147    #[error("Failed to read float: {0}")]
148    ReadFloat(#[source] std::io::Error),
149
150    #[error("Failed to read duration: {0}")]
151    ReadDuration(#[source] std::io::Error),
152
153    #[error("Failed to read fixed number of bytes '{1}': : {0}")]
154    ReadFixed(#[source] std::io::Error, usize),
155
156    #[error("Failed to convert &str to UUID: {0}")]
157    ConvertStrToUuid(#[source] uuid::Error),
158
159    #[error("Failed to convert Fixed bytes to UUID. It must be exactly 16 bytes, got {0}")]
160    ConvertFixedToUuid(usize),
161
162    #[error("Failed to convert Bytes to UUID. It must be exactly 16 bytes, got {0}")]
163    ConvertBytesToUuid(usize),
164
165    #[error("Failed to convert String to UUID. Expected at most {0} bytes, got {0}")]
166    ConvertStringToUuid(usize, usize),
167
168    #[error("Failed to convert Fixed bytes to UUID: {0}")]
169    ConvertSliceToUuid(#[source] uuid::Error),
170
171    #[error("Map key is not a string; key type is {0:?}")]
172    MapKeyType(ValueKind),
173
174    #[error("Union index {index} out of bounds: {num_variants}")]
175    GetUnionVariant { index: i64, num_variants: usize },
176
177    #[error(
178        "Enum symbol index out of bounds: got {index} but there are only {num_variants} variants"
179    )]
180    EnumSymbolIndex { index: usize, num_variants: usize },
181
182    #[error("Enum symbol not found {0}")]
183    GetEnumSymbol(String),
184
185    #[error("Unable to decode enum index")]
186    GetEnumUnknownIndexValue,
187
188    #[error("Scale {scale} is greater than precision {precision}")]
189    GetScaleAndPrecision {
190        scale: usize,
191        precision: NonZero<usize>,
192    },
193
194    #[error(
195        "Fixed type number of bytes {size} is not large enough to hold decimal values of precision {precision}"
196    )]
197    GetScaleWithFixedSize {
198        size: usize,
199        precision: NonZero<usize>,
200    },
201
202    #[error("Expected Value::Uuid, got: {0:?}")]
203    GetUuid(Value),
204
205    #[error("Expected Value::BigDecimal, got: {0:?}")]
206    GetBigDecimal(Value),
207
208    #[error("Fixed bytes of size 12 expected, got Fixed of size {0}")]
209    GetDurationFixedBytes(usize),
210
211    #[error("Expected Value::Duration or Value::Fixed(12), got: {0:?}")]
212    ResolveDuration(Value),
213
214    #[error("Expected Value::Decimal, Value::Bytes, Value::Fixed or Value::String, got: {0:?}")]
215    ResolveDecimal(Value),
216
217    #[error("Missing field in record: {0:?}")]
218    GetField(String),
219
220    #[error("Unable to convert to u8, got {0:?}")]
221    GetU8(Value),
222
223    #[error("Precision {precision} too small to hold decimal values with {num_bytes} bytes")]
224    ComparePrecisionAndSize {
225        precision: NonZero<usize>,
226        num_bytes: usize,
227    },
228
229    #[error("Cannot convert length to i32: {1}")]
230    ConvertLengthToI32(#[source] std::num::TryFromIntError, usize),
231
232    #[error("Expected Value::Date or Value::Int, got: {0:?}")]
233    GetDate(Value),
234
235    #[error("Expected Value::TimeMillis or Value::Int, got: {0:?}")]
236    GetTimeMillis(Value),
237
238    #[error("Expected Value::TimeMicros, Value::Long or Value::Int, got: {0:?}")]
239    GetTimeMicros(Value),
240
241    #[error("Expected Value::TimestampMillis, Value::Long or Value::Int, got: {0:?}")]
242    GetTimestampMillis(Value),
243
244    #[error("Expected Value::TimestampMicros, Value::Long or Value::Int, got: {0:?}")]
245    GetTimestampMicros(Value),
246
247    #[error("Expected Value::TimestampNanos, Value::Long or Value::Int, got: {0:?}")]
248    GetTimestampNanos(Value),
249
250    #[error("Expected Value::LocalTimestampMillis, Value::Long or Value::Int, got: {0:?}")]
251    GetLocalTimestampMillis(Value),
252
253    #[error("Expected Value::LocalTimestampMicros, Value::Long or Value::Int, got: {0:?}")]
254    GetLocalTimestampMicros(Value),
255
256    #[error("Expected Value::LocalTimestampNanos, Value::Long or Value::Int, got: {0:?}")]
257    GetLocalTimestampNanos(Value),
258
259    #[error("Expected Value::Null, got: {0:?}")]
260    GetNull(Value),
261
262    #[error("Expected Value::Boolean, got: {0:?}")]
263    GetBoolean(Value),
264
265    #[error("Expected Value::Int, got: {0:?}")]
266    GetInt(Value),
267
268    #[error("Expected Value::Long or Value::Int, got: {0:?}")]
269    GetLong(Value),
270
271    #[error(r#"Expected Value::Double, Value::Float, Value::Int, Value::Long or Value::String ("NaN", "INF", "Infinity", "-INF" or "-Infinity"), got: {0:?}"#)]
272    GetDouble(Value),
273
274    #[error(r#"Expected Value::Float, Value::Double, Value::Int, Value::Long or Value::String ("NaN", "INF", "Infinity", "-INF" or "-Infinity"), got: {0:?}"#)]
275    GetFloat(Value),
276
277    #[error("Expected Value::Bytes, got: {0:?}")]
278    GetBytes(Value),
279
280    #[error("Expected Value::String, Value::Bytes or Value::Fixed, got: {0:?}")]
281    GetString(Value),
282
283    #[error("Expected Value::Enum, got: {0:?}")]
284    GetEnum(Value),
285
286    #[error("Fixed size mismatch, expected: {size}, got: {n}")]
287    CompareFixedSizes { size: usize, n: usize },
288
289    #[error("String expected for fixed, got: {0:?}")]
290    GetStringForFixed(Value),
291
292    #[error("Enum default {symbol:?} is not among allowed symbols {symbols:?}")]
293    GetEnumDefault {
294        symbol: String,
295        symbols: Vec<String>,
296    },
297
298    #[error("Enum value index {index} is out of bounds {nsymbols}")]
299    GetEnumValue { index: usize, nsymbols: usize },
300
301    #[error("Key {0} not found in decimal metadata JSON")]
302    GetDecimalMetadataFromJson(&'static str),
303
304    #[error("Could not find matching type in {schema:?} for {value:?}")]
305    FindUnionVariant { schema: UnionSchema, value: Value },
306
307    #[error("Union type should not be empty")]
308    EmptyUnion,
309
310    #[error("Array({expected:?}) expected, got {other:?}")]
311    GetArray { expected: SchemaKind, other: Value },
312
313    #[error("Map({expected:?}) expected, got {other:?}")]
314    GetMap { expected: SchemaKind, other: Value },
315
316    #[error("Record with fields {expected:?} expected, got {other:?}")]
317    GetRecord {
318        expected: Vec<(String, SchemaKind)>,
319        other: Value,
320    },
321
322    #[error("No `name` field")]
323    GetNameField,
324
325    #[error("Expected a string for the `namespace` field, got a {0}")]
326    GetNamespaceFieldWrongType(&'static str),
327
328    #[error("No `name` in record field")]
329    GetNameFieldFromRecord,
330
331    #[error("Unions may not directly contain a union")]
332    GetNestedUnion,
333
334    #[error(
335        "Found two different maps while building Union: Schema::Map({0:?}), Schema::Map({1:?})"
336    )]
337    GetUnionDuplicateMap(Schema, Schema),
338
339    #[error(
340        "Found two different arrays while building Union: Schema::Array({0:?}), Schema::Array({1:?})"
341    )]
342    GetUnionDuplicateArray(Schema, Schema),
343
344    #[error("Unions cannot contain duplicate types, found at least two {0:?}")]
345    GetUnionDuplicate(SchemaKind),
346
347    #[error("Unions cannot contain more than one named schema with the same name: {0}")]
348    GetUnionDuplicateNamedSchemas(String),
349
350    #[error("One union type {0:?} must match the `default`'s value type {1:?}")]
351    GetDefaultUnion(SchemaKind, ValueKind),
352
353    #[error("`default`'s value type of field `{0}` in `{1}` must be a `{2:#}`. Got: {3:?}")]
354    GetDefaultRecordField(String, String, String, serde_json::Value),
355
356    #[error("JSON number {0} could not be converted into an Avro value as it's too large")]
357    JsonNumberTooLarge(serde_json::Number),
358
359    #[error("JSON value {0} claims to be u64 but cannot be converted")]
360    GetU64FromJson(serde_json::Number),
361
362    #[error("JSON value {0} claims to be i64 but cannot be converted")]
363    GetI64FromJson(serde_json::Number),
364
365    #[error("Cannot convert u64 to usize: {1}")]
366    ConvertU64ToUsize(#[source] std::num::TryFromIntError, u64),
367
368    #[deprecated(since = "0.20.0", note = "This error variant is not generated anymore")]
369    #[error("Cannot convert u32 to usize: {1}")]
370    ConvertU32ToUsize(#[source] std::num::TryFromIntError, u32),
371
372    #[error("Cannot convert i64 to usize: {1}")]
373    ConvertI64ToUsize(#[source] std::num::TryFromIntError, i64),
374
375    #[error("Cannot convert i32 to usize: {1}")]
376    ConvertI32ToUsize(#[source] std::num::TryFromIntError, i32),
377
378    #[error("Cannot convert i64 to u64: {1}")]
379    ConvertI64ToU64(#[source] std::num::TryFromIntError, i64),
380
381    #[error("Cannot convert i32 to u64: {1}")]
382    ConvertI32ToU64(#[source] std::num::TryFromIntError, i32),
383
384    #[error("Cannot convert i64 to u128: {1}")]
385    ConvertI64ToU128(#[source] std::num::TryFromIntError, i64),
386
387    #[error("Cannot convert i32 to u128: {1}")]
388    ConvertI32ToU128(#[source] std::num::TryFromIntError, i32),
389
390    #[error("Cannot convert usize to i64: {1}")]
391    ConvertUsizeToI64(#[source] std::num::TryFromIntError, usize),
392
393    #[error("Invalid JSON value for decimal precision/scale integer: {0}")]
394    GetPrecisionOrScaleFromJson(serde_json::Number),
395
396    #[error("Failed to parse schema from JSON")]
397    ParseSchemaJson(#[source] serde_json::Error),
398
399    #[error("Failed to read schema")]
400    ReadSchemaFromReader(#[source] std::io::Error),
401
402    #[error("Must be a JSON string, object or array")]
403    ParseSchemaFromValidJson,
404
405    #[error("Unknown primitive type: {0}")]
406    ParsePrimitive(String),
407
408    #[error("Unknown primitive type: '{0}'. Did you mean '{1}' ?")]
409    ParsePrimitiveSimilar(String, &'static str),
410
411    #[error("invalid JSON for {key:?}: {value:?}")]
412    GetDecimalMetadataValueFromJson {
413        key: String,
414        value: serde_json::Value,
415    },
416
417    #[error("The decimal precision ({precision}) must be bigger or equal to the scale ({scale})")]
418    DecimalPrecisionLessThanScale {
419        precision: NonZero<usize>,
420        scale: usize,
421    },
422
423    #[error("The decimal precision ({precision}) must be a non-zero positive number")]
424    DecimalPrecisionMuBePositive { precision: usize },
425
426    #[deprecated(since = "0.20.0", note = "This error variant is not generated anymore")]
427    #[error("Unreadable big decimal sign")]
428    BigDecimalSign,
429
430    #[error("Unreadable length for big decimal inner bytes: {0}")]
431    BigDecimalLen(#[source] Box<Error>),
432
433    #[error("Unreadable big decimal scale")]
434    BigDecimalScale,
435
436    #[deprecated(since = "0.20.0", note = "This error variant is not generated anymore")]
437    #[error("Unexpected `type` {0} variant for `logicalType`")]
438    GetLogicalTypeVariant(serde_json::Value),
439
440    #[error("No `type` field found for `logicalType`")]
441    GetLogicalTypeField,
442
443    #[error("logicalType must be a string, but is {0:?}")]
444    GetLogicalTypeFieldType(serde_json::Value),
445
446    #[error("Unknown complex type: {0}")]
447    GetComplexType(serde_json::Value),
448
449    #[error("No `type` in complex type")]
450    GetComplexTypeField,
451
452    #[error("No `type` in record field")]
453    GetRecordFieldTypeField,
454
455    #[error("No `fields` in record")]
456    GetRecordFieldsJson,
457
458    #[error("Expected an object in the array of the `fields` field, got a {0}")]
459    GetRecordFieldsArrayInvalidType(&'static str),
460
461    #[error("Expected an array of objects for the `fields` field, got a {0}")]
462    GetRecordFieldsInvalidType(&'static str),
463
464    #[error("No `symbols` field in enum")]
465    GetEnumSymbolsField,
466
467    #[error("Expected an array of strings for the `symbols` field, got a {0}")]
468    GetEnumSymbolsFieldInvalidType(&'static str),
469
470    #[error("Expected a string in the array of the `symbols` field, got a {0}")]
471    GetEnumSymbolsFieldArrayInvalidType(&'static str),
472
473    #[error("Unable to parse `symbols` in enum")]
474    GetEnumSymbols,
475
476    #[error("Invalid enum symbol name {0}")]
477    EnumSymbolName(String),
478
479    #[error("Invalid field name {0}")]
480    FieldName(String),
481
482    #[error("Duplicate field name {0}")]
483    FieldNameDuplicate(String),
484
485    #[error("Invalid schema name {0}. It must match the regex '{1}'")]
486    InvalidSchemaName(String, &'static str),
487
488    #[error("Invalid namespace {0}. It must match the regex '{1}'")]
489    InvalidNamespace(String, &'static str),
490
491    #[error(
492        "Invalid schema: There is no type called '{0}', if you meant to define a non-primitive schema, it should be defined inside `type` attribute."
493    )]
494    InvalidSchemaRecord(String),
495
496    #[error("Duplicate enum symbol {0}")]
497    EnumSymbolDuplicate(String),
498
499    #[error("Default value for an enum must be a string! Got: {0}")]
500    EnumDefaultWrongType(serde_json::Value),
501
502    #[error("Default value for an array must be an array! Got: {0}")]
503    ArrayDefaultWrongType(serde_json::Value),
504
505    #[error("Default value for an array must be an array of {0}! Found: {1:?}")]
506    ArrayDefaultWrongInnerType(Schema, Value),
507
508    #[error("Default value for a map must be an object! Got: {0}")]
509    MapDefaultWrongType(serde_json::Value),
510
511    #[error("Default value for a map must be an object with (String, {0})! Found: (String, {1:?})")]
512    MapDefaultWrongInnerType(Schema, Value),
513
514    #[error("No `items` in array")]
515    GetArrayItemsField,
516
517    #[error("No `values` in map")]
518    GetMapValuesField,
519
520    #[error("Fixed schema `size` value must be a positive integer: {0}")]
521    GetFixedSizeFieldPositive(serde_json::Value),
522
523    #[error("Fixed schema has no `size`")]
524    GetFixedSizeField,
525
526    #[error("Expected an unsigned integer for the `size` field, got a {0}")]
527    GetFixedSizeFieldInvalidType(&'static str),
528
529    #[error("Expected an array of strings for the `aliases` field, got a {0}")]
530    GetAliasesFieldInvalidType(&'static str),
531
532    #[error("Expected a string in the array for the `aliases` field, got a {0}")]
533    GetAliasesFieldArrayInvalidType(&'static str),
534
535    #[error("Expected a string for the `{0}` field, got a {1}")]
536    GetStringInvalidType(&'static str, &'static str),
537
538    #[deprecated(since = "0.22.0", note = "This error variant is not generated anymore")]
539    #[error("Fixed schema's default value length ({0}) does not match its size ({1})")]
540    FixedDefaultLenSizeMismatch(usize, u64),
541
542    #[deprecated(since = "0.20.0", note = "This error variant is not generated anymore")]
543    #[error("Failed to compress with flate: {0}")]
544    DeflateCompress(#[source] std::io::Error),
545
546    // no longer possible after migration from libflate to miniz_oxide
547    #[deprecated(since = "0.19.0", note = "This error can no longer occur")]
548    #[error("Failed to finish flate compressor: {0}")]
549    DeflateCompressFinish(#[source] std::io::Error),
550
551    #[error("Failed to decompress with flate: {0}")]
552    DeflateDecompress(#[source] std::io::Error),
553
554    #[cfg(feature = "snappy")]
555    #[error("Failed to compress with snappy: {0}")]
556    SnappyCompress(#[source] snap::Error),
557
558    #[cfg(feature = "snappy")]
559    #[error("Failed to get snappy decompression length: {0}")]
560    GetSnappyDecompressLen(#[source] snap::Error),
561
562    #[cfg(feature = "snappy")]
563    #[error("Snappy-compressed block is {0} bytes, too short to contain the trailing CRC32")]
564    BadSnappyLength(usize),
565
566    #[cfg(feature = "snappy")]
567    #[error("Failed to decompress with snappy: {0}")]
568    SnappyDecompress(#[source] snap::Error),
569
570    #[error("Failed to compress with zstd: {0}")]
571    ZstdCompress(#[source] std::io::Error),
572
573    #[error("Failed to decompress with zstd: {0}")]
574    ZstdDecompress(#[source] std::io::Error),
575
576    #[cfg(feature = "bzip")]
577    #[error("Failed to decompress with bzip2: {0}")]
578    Bzip2Decompress(#[source] std::io::Error),
579
580    #[cfg(feature = "xz")]
581    #[error("Failed to decompress with xz: {0}")]
582    XzDecompress(#[source] std::io::Error),
583
584    #[error("Failed to read header: {0}")]
585    ReadHeader(#[source] std::io::Error),
586
587    #[error("wrong magic in header")]
588    HeaderMagic,
589
590    #[error("Message Header mismatch. Expected: {0:?}. Actual: {1:?}")]
591    SingleObjectHeaderMismatch(Vec<u8>, Vec<u8>),
592
593    #[error("Failed to get JSON from avro.schema key in map")]
594    GetAvroSchemaFromMap,
595
596    #[error("no metadata in header")]
597    GetHeaderMetadata,
598
599    #[error("Failed to read marker bytes: {0}")]
600    ReadMarker(#[source] std::io::Error),
601
602    #[error("Failed to read block marker bytes: {0}")]
603    ReadBlockMarker(#[source] std::io::Error),
604
605    #[error("Read into buffer failed: {0}")]
606    ReadIntoBuf(#[source] std::io::Error),
607
608    #[error(
609        "Invalid sync marker! The sync marker in the data block \
610        doesn't match the file header's sync marker. This likely \
611        indicates data corruption, truncated file, or incorrectly \
612        concatenated Avro files. Verify file integrity and ensure \
613        proper file transmission or creation."
614    )]
615    GetBlockMarker,
616
617    #[error("Overflow when decoding integer value")]
618    IntegerOverflow,
619
620    #[error(
621        "Maximum decode recursion depth reached (maximum: {maximum}). Change the limit using `apache_avro::util::max_decode_recursion_depth`"
622    )]
623    DecodeRecursionLimit { maximum: usize },
624
625    #[error("Failed to read bytes for decoding variable length integer: {0}")]
626    ReadVariableIntegerBytes(#[source] std::io::Error),
627
628    #[error("Decoded integer out of range for i32: {1}: {0}")]
629    ZagI32(#[source] std::num::TryFromIntError, i64),
630
631    #[error("Did not read any bytes, block is corrupt")]
632    ReadBlock,
633
634    #[error("Failed to serialize value into Avro value: {0}")]
635    SerializeValue(String),
636
637    #[error("Failed to serialize value of type `{value_type}` using Schema::{schema:?}: {value}")]
638    SerializeValueWithSchema {
639        value_type: &'static str,
640        value: String,
641        schema: Schema,
642    },
643
644    #[error("{position} is not a valid index for fields in {schema:?}")]
645    SerializeRecordUnknownFieldIndex {
646        position: usize,
647        schema: RecordSchema,
648    },
649
650    #[error("Failed to serialize field '{field_name}' of record {record_schema:?}: {error}")]
651    SerializeRecordFieldWithSchema {
652        field_name: String,
653        record_schema: RecordSchema,
654        error: String,
655    },
656
657    #[error("Missing default for skipped field '{field_name}' of schema {schema:?}")]
658    MissingDefaultForSkippedField {
659        field_name: String,
660        schema: RecordSchema,
661    },
662
663    #[error("Failed to deserialize Avro value into value: {0}")]
664    DeserializeValue(String),
665
666    #[error("Failed to deserialize value of type {value_type} using schema {schema:?}: {value}")]
667    DeserializeSchemaAware {
668        value_type: &'static str,
669        value: String,
670        schema: Schema,
671    },
672
673    #[error("Only expected `deserialize_identifier` to be called but `{0}` was called")]
674    DeserializeIdentifier(&'static str),
675
676    #[error("Failed to write buffer bytes during flush: {0}")]
677    WriteBytes(#[source] std::io::Error),
678
679    #[error("Failed to flush inner writer during flush: {0}")]
680    FlushWriter(#[source] std::io::Error),
681
682    #[error("Failed to write marker: {0}")]
683    WriteMarker(#[source] std::io::Error),
684
685    #[error("Failed to convert JSON to string: {0}")]
686    ConvertJsonToString(#[source] serde_json::Error),
687
688    /// Error while converting float to JSON value
689    #[error("failed to convert avro float to json: {0}")]
690    ConvertF64ToJson(f64),
691
692    /// Error while resolving [`Schema::Ref`]
693    #[error("Unresolved schema reference: {0}")]
694    SchemaResolutionError(Name),
695
696    #[error("The file metadata is already flushed.")]
697    FileHeaderAlreadyWritten,
698
699    #[error("Metadata keys starting with 'avro.' are reserved for internal usage: {0}.")]
700    InvalidMetadataKey(String),
701
702    /// Error when two named schema have the same fully qualified name
703    #[error("Two named schema defined for same fullname: {0}.")]
704    AmbiguousSchemaDefinition(Name),
705
706    #[error("Signed decimal bytes length {0} not equal to fixed schema size {1}.")]
707    EncodeDecimalAsFixedError(usize, usize),
708
709    #[error("There is no entry for '{0}' in the lookup table: {1}.")]
710    NoEntryInLookupTable(String, String),
711
712    #[error("Can only encode value type {value_kind:?} as one of {supported_schema:?}")]
713    EncodeValueAsSchemaError {
714        value_kind: ValueKind,
715        supported_schema: Vec<SchemaKind>,
716    },
717    #[error("Internal buffer not drained properly. Re-initialize the single object writer struct!")]
718    IllegalSingleObjectWriterState,
719
720    #[error("Codec '{0}' is not supported/enabled")]
721    CodecNotSupported(String),
722
723    #[error("Invalid Avro data! Cannot read codec type from value that is not Value::Bytes.")]
724    BadCodecMetadata,
725
726    #[error("Cannot convert a slice to Uuid: {0}")]
727    UuidFromSlice(#[source] uuid::Error),
728
729    #[error("Expected String for Map key when serializing a flattened struct")]
730    MapFieldExpectedString,
731
732    #[error("No key for value when serializing a map")]
733    MapNoKey,
734
735    #[error(
736        "The implementation of `SchemaNameValidator` is incorrect! It returned an out-of-bounds index or provided a regex that did not capture a group named `name`"
737    )]
738    InvalidSchemaNameValidatorImplementation,
739
740    #[error(
741        "Not all tuple fields were serialized, expected to serialize element at position {position} of a {total_elements}-tuple but `SerializeTuple::end()` was called"
742    )]
743    SerializeTupleMissingElements {
744        position: usize,
745        total_elements: usize,
746    },
747
748    #[error("The value is invalid for the given schema: {0}")]
749    InvalidValueForSchema(String),
750
751    #[error("The value is invalid for all the given schemas: {0:?}")]
752    InvalidValueForAllSchemas(Vec<String>),
753}
754
755#[derive(thiserror::Error, PartialEq)]
756pub enum CompatibilityError {
757    #[error(
758        "Incompatible schema types! Writer schema is '{writer_schema_type}', but reader schema is '{reader_schema_type}'"
759    )]
760    WrongType {
761        writer_schema_type: String,
762        reader_schema_type: String,
763    },
764
765    #[error("Incompatible schema types! The {schema_type} should have been {expected_type:?}")]
766    TypeExpected {
767        schema_type: String,
768        expected_type: Vec<SchemaKind>,
769    },
770
771    #[error(
772        "Incompatible schemata! Field '{0}' in reader schema does not match the type in the writer schema"
773    )]
774    FieldTypeMismatch(String, #[source] Box<CompatibilityError>),
775
776    #[error("Incompatible schemata! Field '{0}' in reader schema must have a default value")]
777    MissingDefaultValue(String),
778
779    #[error("Incompatible schemata! Reader's symbols contain none of the writer's symbols")]
780    MissingSymbols,
781
782    #[error("Incompatible schemata! All elements in union must match for both schemas")]
783    MissingUnionElements,
784
785    #[error("Incompatible schemata! At least one element in the union must match the schema")]
786    SchemaMismatchAllUnionElements,
787
788    #[error("Incompatible schemata! Size doesn't match for fixed")]
789    FixedMismatch,
790
791    #[error(
792        "Incompatible schemata! Decimal precision and/or scale don't match, reader: ({r_precision},{r_scale}), writer: ({w_precision},{w_scale})"
793    )]
794    DecimalMismatch {
795        r_precision: NonZero<usize>,
796        r_scale: usize,
797        w_precision: NonZero<usize>,
798        w_scale: usize,
799    },
800
801    #[error(
802        "Incompatible schemata! The name must be the same for both schemas. Writer's name {writer_name} and reader's name {reader_name}"
803    )]
804    NameMismatch {
805        writer_name: String,
806        reader_name: String,
807    },
808
809    #[error(
810        "Incompatible schemata! Unknown type for '{0}'. Make sure that the type is a valid one"
811    )]
812    Inconclusive(String),
813}
814
815impl serde::ser::Error for Details {
816    fn custom<T: fmt::Display>(msg: T) -> Self {
817        Details::SerializeValue(msg.to_string())
818    }
819}
820
821impl serde::de::Error for Details {
822    fn custom<T: fmt::Display>(msg: T) -> Self {
823        Details::DeserializeValue(msg.to_string())
824    }
825}
826
827impl fmt::Debug for Details {
828    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
829        let mut msg = self.to_string();
830        if let Some(e) = self.source() {
831            msg.extend([": ", &e.to_string()]);
832        }
833        write!(f, "{msg}")
834    }
835}
836
837impl fmt::Debug for CompatibilityError {
838    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
839        let mut msg = self.to_string();
840        if let Some(e) = self.source() {
841            msg.extend([": ", &e.to_string()]);
842        }
843        write!(f, "{msg}")
844    }
845}