Skip to main content

linera_views/
error.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4/// Main error type for the crate.
5#[derive(thiserror::Error, Debug)]
6pub enum ViewError {
7    /// BCS serialization error.
8    #[error(transparent)]
9    BcsError(#[from] bcs::Error),
10
11    /// Input output error.
12    #[error("I/O error")]
13    IoError(#[from] std::io::Error),
14
15    /// Arithmetic error
16    #[error(transparent)]
17    ArithmeticError(#[from] linera_base::data_types::ArithmeticError),
18
19    /// Failed to lock a reentrant collection entry since it is currently being accessed
20    #[error(
21        "failed to lock a reentrant collection entry since it is currently being accessed: {0:?}"
22    )]
23    TryLockError(Vec<u8>),
24
25    /// Tokio errors can happen while joining.
26    #[error("panic in sub-task: {0}")]
27    TokioJoinError(#[from] tokio::task::JoinError),
28
29    /// Errors within the context can occur and are presented as `ViewError`.
30    #[error("storage operation error in {backend}: {error}")]
31    StoreError {
32        /// The name of the backend that produced the error
33        backend: &'static str,
34        /// The inner error
35        #[source]
36        error: Box<dyn std::error::Error + Send + Sync>,
37        /// Whether this error may have left storage in an undetermined state,
38        /// so the view must be reloaded before being used again.
39        must_reload_view: bool,
40    },
41
42    /// The key must not be too long
43    #[error("the key must not be too long")]
44    KeyTooLong,
45
46    /// The entry does not exist in memory
47    // FIXME(#148): This belongs to a future `linera_storage::StoreError`.
48    #[error("entry does not exist in storage: {0}")]
49    NotFound(String),
50
51    /// The database is corrupt: Entries don't have the expected hash.
52    #[error("inconsistent database entries")]
53    InconsistentEntries,
54
55    /// The database is corrupt: Some entries are missing
56    #[error("missing database entries for the context  {0}")]
57    MissingEntries(String),
58
59    /// The values are incoherent.
60    #[error("post load values error")]
61    PostLoadValuesError,
62
63    /// The operation requires the view to have no pending in-memory changes.
64    #[error("the view has pending in-memory changes; flush them before continuing")]
65    HasPendingChanges,
66
67    /// The canonical byte stream is malformed.
68    #[error("malformed canonical content stream: {0}")]
69    MalformedContent(&'static str),
70}
71
72impl ViewError {
73    /// Returns `true` if this error may have left storage in an undetermined state,
74    /// so the view must be reloaded before being used again.
75    pub fn must_reload_view(&self) -> bool {
76        matches!(
77            self,
78            ViewError::StoreError {
79                must_reload_view: true,
80                ..
81            }
82        )
83    }
84}