Skip to main content

lance_nd/
error.rs

1//! Error type for lance-nd.
2
3use std::sync::Arc;
4
5use arrow::error::ArrowError;
6use nd_arrow_array::NdError;
7
8/// Errors raised by lance-nd.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// An error from the nd array encoding.
13    #[error(transparent)]
14    Nd(#[from] NdError),
15    /// An error from Lance.
16    #[error(transparent)]
17    Lance(Box<lance::Error>),
18    /// An error from Arrow.
19    #[error(transparent)]
20    Arrow(#[from] ArrowError),
21    /// Variable names or types do not match.
22    #[error("schema mismatch: {0}")]
23    SchemaMismatch(String),
24    /// The Lance dataset does not follow the nd-arrow encoding, or has
25    /// another format version.
26    #[error("invalid table: {0}")]
27    InvalidTable(String),
28    /// An argument is not valid.
29    #[error("invalid argument: {0}")]
30    InvalidArgument(String),
31    /// No table exists at the uri.
32    #[error("no table at {0}")]
33    TableNotFound(String),
34    /// A table exists at the uri.
35    #[error("a table exists at {0}")]
36    TableExists(String),
37    /// Another commit changed the table first. When `retryable` is `true`,
38    /// the same operation can succeed when you try it again.
39    #[error("commit conflict: {source}")]
40    CommitConflict {
41        /// `true` when a new try can succeed.
42        retryable: bool,
43        /// The Lance error.
44        source: Box<lance::Error>,
45    },
46    /// A write of an [`crate::NdWriter`] failed, so the writer commits nothing.
47    #[error("the writer failed, nothing is committed: {0}")]
48    WriterFailed(#[source] Arc<Error>),
49    /// A fragment write task stopped without a result.
50    #[error("a fragment write stopped: {0}")]
51    TaskFailed(#[from] tokio::task::JoinError),
52}
53
54impl From<lance::Error> for Error {
55    fn from(error: lance::Error) -> Self {
56        match error {
57            lance::Error::DatasetNotFound { path, .. } => Self::TableNotFound(path),
58            lance::Error::DatasetAlreadyExists { uri, .. } => Self::TableExists(uri),
59            lance::Error::CommitConflict { .. }
60            | lance::Error::RetryableCommitConflict { .. }
61            | lance::Error::TooMuchWriteContention { .. } => Self::CommitConflict {
62                retryable: true,
63                source: Box::new(error),
64            },
65            lance::Error::IncompatibleTransaction { .. } => Self::CommitConflict {
66                retryable: false,
67                source: Box::new(error),
68            },
69            error => Self::Lance(Box::new(error)),
70        }
71    }
72}
73
74/// A result with [`Error`] as the default error.
75pub type Result<T, E = Error> = std::result::Result<T, E>;