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 was caused by a journal resolution failure.
38        must_reload_view: bool,
39    },
40
41    /// The key must not be too long
42    #[error("the key must not be too long")]
43    KeyTooLong,
44
45    /// The entry does not exist in memory
46    // FIXME(#148): This belongs to a future `linera_storage::StoreError`.
47    #[error("entry does not exist in storage: {0}")]
48    NotFound(String),
49
50    /// The database is corrupt: Entries don't have the expected hash.
51    #[error("inconsistent database entries")]
52    InconsistentEntries,
53
54    /// The database is corrupt: Some entries are missing
55    #[error("missing database entries for the context  {0}")]
56    MissingEntries(String),
57
58    /// The values are incoherent.
59    #[error("post load values error")]
60    PostLoadValuesError,
61
62    /// The operation requires the view to have no pending in-memory changes.
63    #[error("the view has pending in-memory changes; flush them before continuing")]
64    HasPendingChanges,
65
66    /// The canonical byte stream is malformed.
67    #[error("malformed canonical content stream: {0}")]
68    MalformedContent(&'static str),
69}
70
71impl ViewError {
72    /// Returns `true` if this error was caused by a journal resolution failure,
73    /// which may leave storage in an inconsistent state requiring a view reload.
74    pub fn must_reload_view(&self) -> bool {
75        matches!(
76            self,
77            ViewError::StoreError {
78                must_reload_view: true,
79                ..
80            }
81        )
82    }
83}