olai_store/error.rs
1/// A convenience type for declaring Results in the resource store.
2pub type Result<T, E = Error> = std::result::Result<T, E>;
3
4/// Errors returned by the resource store and association store.
5///
6/// Store operations report failures through these variants; the doc comments on
7/// the [`ObjectStore`] and [`AssociationStore`] trait methods note which variant
8/// a given method returns and when.
9///
10/// [`ObjectStore`]: crate::ObjectStore
11/// [`AssociationStore`]: crate::AssociationStore
12#[derive(Debug, thiserror::Error)]
13pub enum Error {
14 #[error("Entity not found.")]
15 NotFound,
16
17 #[error("Entity already exists.")]
18 AlreadyExists,
19
20 #[error("Precondition failed: the object was modified concurrently.")]
21 Conflict,
22
23 #[error("Invalid argument: {0}")]
24 InvalidArgument(String),
25
26 #[error("Invalid identifier: {0}")]
27 InvalidIdentifier(#[from] uuid::Error),
28
29 #[error("Generic error: {0}")]
30 Generic(String),
31
32 #[error(transparent)]
33 SerDe(#[from] serde_json::Error),
34}
35
36impl Error {
37 /// Constructs an [`Error::Generic`] from a message.
38 ///
39 /// Use this for failures that do not map onto a more specific variant.
40 pub fn generic(msg: impl Into<String>) -> Self {
41 Self::Generic(msg.into())
42 }
43
44 /// Constructs an [`Error::InvalidArgument`] from a message.
45 ///
46 /// Use this when a caller-supplied argument is malformed or out of range.
47 pub fn invalid_argument(msg: impl Into<String>) -> Self {
48 Self::InvalidArgument(msg.into())
49 }
50
51 /// A stable, low-cardinality kind string for use as a `tracing` / OpenTelemetry
52 /// `error.type` field.
53 ///
54 /// Returns a `&'static str` and never includes an inner message, so recording it on a
55 /// span cannot leak the caller-supplied text carried by [`InvalidArgument`](Self::InvalidArgument)
56 /// or [`Generic`](Self::Generic).
57 pub(crate) fn kind_str(&self) -> &'static str {
58 match self {
59 Error::NotFound => "not_found",
60 Error::AlreadyExists => "already_exists",
61 Error::Conflict => "conflict",
62 Error::InvalidArgument(_) => "invalid_argument",
63 Error::InvalidIdentifier(_) => "invalid_identifier",
64 Error::Generic(_) => "generic",
65 Error::SerDe(_) => "serde",
66 }
67 }
68}