Skip to main content

arrow_avro/
errors.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Common Avro errors and macros.
6
7use arrow_schema::ArrowError;
8use core::num::TryFromIntError;
9use std::error::Error;
10use std::string::FromUtf8Error;
11use std::{io, str};
12
13/// Avro error enumeration
14
15#[derive(Debug)]
16#[non_exhaustive]
17pub enum AvroError {
18    /// General Avro error.
19    /// Returned when code violates normal workflow of working with Avro data.
20    General(String),
21    /// "Not yet implemented" Avro error.
22    /// Returned when functionality is not yet available.
23    NYI(String),
24    /// "End of file" Avro error.
25    /// Returned when IO related failures occur, e.g. when there are not enough bytes to
26    /// decode.
27    EOF(String),
28    /// Arrow error.
29    /// Returned when reading into arrow or writing from arrow.
30    ArrowError(Box<ArrowError>),
31    /// Error when the requested index is more than the
32    /// number of items expected
33    IndexOutOfBound(usize, usize),
34    /// Error indicating that an unexpected or bad argument was passed to a function.
35    InvalidArgument(String),
36    /// Error indicating that a value could not be parsed.
37    ParseError(String),
38    /// Error indicating that a schema is invalid.
39    SchemaError(String),
40    /// An external error variant
41    External(Box<dyn Error + Send + Sync>),
42    /// Error during IO operations
43    IoError(String, io::Error),
44    /// Returned when a function needs more data to complete properly. The `usize` field indicates
45    /// the total number of bytes required, not the number of additional bytes.
46    NeedMoreData(usize),
47    /// Returned when a function needs more data to complete properly.
48    /// The `Range<u64>` indicates the range of bytes that are needed.
49    NeedMoreDataRange(std::ops::Range<u64>),
50    /// Returned when an unframed datum cannot be decoded until the current batch is flushed.
51    BatchFull,
52}
53
54impl std::fmt::Display for AvroError {
55    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
56        match &self {
57            AvroError::General(message) => {
58                write!(fmt, "Avro error: {message}")
59            }
60            AvroError::NYI(message) => write!(fmt, "NYI: {message}"),
61            AvroError::EOF(message) => write!(fmt, "EOF: {message}"),
62            AvroError::ArrowError(message) => write!(fmt, "Arrow: {message}"),
63            AvroError::IndexOutOfBound(index, bound) => {
64                write!(fmt, "Index {index} out of bound: {bound}")
65            }
66            AvroError::InvalidArgument(message) => {
67                write!(fmt, "Invalid argument: {message}")
68            }
69            AvroError::ParseError(message) => write!(fmt, "Parser error: {message}"),
70            AvroError::SchemaError(message) => write!(fmt, "Schema error: {message}"),
71            AvroError::External(e) => write!(fmt, "External: {e}"),
72            AvroError::IoError(message, e) => write!(fmt, "I/O Error: {message}: {e}"),
73            AvroError::NeedMoreData(needed) => write!(fmt, "NeedMoreData: {needed}"),
74            AvroError::NeedMoreDataRange(range) => {
75                write!(fmt, "NeedMoreDataRange: {}..{}", range.start, range.end)
76            }
77            AvroError::BatchFull => {
78                write!(fmt, "Batch is full; flush before decoding another datum")
79            }
80        }
81    }
82}
83
84impl Error for AvroError {
85    fn source(&self) -> Option<&(dyn Error + 'static)> {
86        match self {
87            AvroError::External(e) => Some(e.as_ref()),
88            AvroError::ArrowError(e) => Some(e.as_ref()),
89            AvroError::IoError(_, e) => Some(e),
90            _ => None,
91        }
92    }
93}
94
95impl From<TryFromIntError> for AvroError {
96    fn from(e: TryFromIntError) -> AvroError {
97        AvroError::General(format!("Integer overflow: {e}"))
98    }
99}
100
101impl From<io::Error> for AvroError {
102    fn from(e: io::Error) -> AvroError {
103        AvroError::External(Box::new(e))
104    }
105}
106
107impl From<str::Utf8Error> for AvroError {
108    fn from(e: str::Utf8Error) -> AvroError {
109        AvroError::External(Box::new(e))
110    }
111}
112
113impl From<FromUtf8Error> for AvroError {
114    fn from(e: FromUtf8Error) -> AvroError {
115        AvroError::External(Box::new(e))
116    }
117}
118
119impl From<ArrowError> for AvroError {
120    fn from(e: ArrowError) -> Self {
121        AvroError::ArrowError(Box::new(e))
122    }
123}
124
125#[cfg(feature = "object_store")]
126impl From<object_store::Error> for AvroError {
127    fn from(e: object_store::Error) -> AvroError {
128        AvroError::External(Box::new(e))
129    }
130}
131
132impl From<AvroError> for io::Error {
133    fn from(e: AvroError) -> Self {
134        io::Error::other(e)
135    }
136}
137
138impl From<AvroError> for ArrowError {
139    fn from(e: AvroError) -> Self {
140        match e {
141            AvroError::External(inner) => ArrowError::from_external_error(inner),
142            AvroError::IoError(msg, err) => ArrowError::IoError(msg, err),
143            AvroError::ArrowError(inner) => *inner,
144            other => ArrowError::AvroError(other.to_string()),
145        }
146    }
147}