Skip to main content

diskann_record/save/
error.rs

1/*
2 * Copyright (c) Microsoft Corporation.
3 * Licensed under the MIT license.
4 */
5
6//! Save-side error type.
7//!
8//! Mirrors the load-side [`super::super::load::Error`] in shape but does not carry a
9//! recoverable / critical distinction: every save failure is terminal because no
10//! probing fallback exists on the writer side.
11
12use std::fmt::{Debug, Display};
13
14/// A specialized [`std::result::Result`] for save-side operations.
15pub type Result<T> = ::std::result::Result<T, Error>;
16
17/// Save-side error.
18///
19/// Wraps [`anyhow::Error`] for rich context chains (see [`Error::context`]) and is
20/// returned from every fallible save-side operation, including [`super::Save::save`]
21/// impls and `save_to_disk`.
22#[derive(Debug)]
23pub struct Error {
24    inner: anyhow::Error,
25}
26
27impl Error {
28    /// Wrap an underlying source error.
29    pub fn new<E>(err: E) -> Self
30    where
31        E: std::error::Error + Send + Sync + 'static,
32    {
33        Error {
34            inner: anyhow::Error::new(err),
35        }
36    }
37
38    /// Construct an error from a display message with no source.
39    pub fn message<D>(message: D) -> Self
40    where
41        D: Display + Debug + Send + Sync + 'static,
42    {
43        Error {
44            inner: anyhow::Error::msg(message),
45        }
46    }
47
48    /// Attach additional context describing what was being attempted.
49    pub fn context<D>(self, message: D) -> Self
50    where
51        D: Display + Send + Sync + 'static,
52    {
53        Error {
54            inner: self.inner.context(message),
55        }
56    }
57}
58
59impl Display for Error {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        write!(f, "Save Error: {:?}", self.inner)
62    }
63}
64
65impl std::error::Error for Error {}