Skip to main content

nostr_database/
error.rs

1// Copyright (c) 2022-2023 Yuki Kishimoto
2// Copyright (c) 2023-2025 Rust Nostr Developers
3// Distributed under the MIT software license
4
5//! Nostr Database Error
6
7opaquerr::define_kind! {
8    /// Nostr database error kind.
9    pub ErrorKind {
10        /// Nostr protocol error.
11        Protocol => "nostr protocol error",
12        /// I/O error.
13        IO => "I/O error",
14        /// Storage error
15        Storage => "storage error",
16        /// Database migration error.
17        Migration => "migration error",
18        /// The operation is known but not supported.
19        Unsupported => "the operation is known but not supported",
20        /// Anything not covered by the stable categories above.
21        Other => "other error",
22    }
23}
24
25opaquerr::define_error! {
26    /// Nostr database error.
27    pub Error(ErrorKind)
28
29    from {
30        nostr::error::Error => ErrorKind::Protocol,
31        std::io::Error => ErrorKind::IO,
32    }
33}
34
35impl Error {
36    /// Storage error
37    pub fn storage<E>(error: E) -> Self
38    where
39        E: Into<Box<dyn std::error::Error + Send + Sync>>,
40    {
41        Self::new(ErrorKind::Storage, error)
42    }
43
44    /// I/O error
45    pub fn io<E>(error: E) -> Self
46    where
47        E: Into<Box<dyn std::error::Error + Send + Sync>>,
48    {
49        Self::new(ErrorKind::IO, error)
50    }
51
52    /// Migration error
53    pub fn migration<E>(error: E) -> Self
54    where
55        E: Into<Box<dyn std::error::Error + Send + Sync>>,
56    {
57        Self::new(ErrorKind::Migration, error)
58    }
59
60    /// unsupported feature
61    pub const fn unsupported(message: &'static str) -> Self {
62        Self::with_static_message(ErrorKind::Unsupported, message)
63    }
64
65    /// Other error
66    pub fn other<E>(error: E) -> Self
67    where
68        E: Into<Box<dyn std::error::Error + Send + Sync>>,
69    {
70        Self::new(ErrorKind::Other, error)
71    }
72}