1use alloy::primitives::B256;
2use signet_storage_types::IntegerListError;
3
4pub type HistoryResult<T, E> = Result<T, HistoryError<E>>;
6
7#[derive(Debug, thiserror::Error)]
12pub enum HistoryError<E: std::error::Error> {
13 #[error("non-contiguous block: expected {expected}, got {got}")]
15 NonContiguousBlock {
16 expected: u64,
18 got: u64,
20 },
21 #[error("parent hash mismatch: expected {expected}, got {got}")]
24 ParentHashMismatch {
25 expected: B256,
27 got: B256,
29 },
30
31 #[error("cannot initialize genesis on a non-empty database")]
33 DbNotEmpty,
34
35 #[error("empty header range provided")]
38 EmptyRange,
39
40 #[error("no blocks in database")]
42 NoBlocks,
43
44 #[error("height {height} outside stored range {first}..={last}")]
46 HeightOutOfRange {
47 height: u64,
49 first: u64,
51 last: u64,
53 },
54
55 #[error("{0}")]
57 Db(#[from] E),
58
59 #[error(transparent)]
61 IntList(IntegerListError),
62}
63
64impl<E: std::error::Error> HistoryError<E> {
65 pub const fn intlist(err: IntegerListError) -> Self {
67 HistoryError::IntList(err)
68 }
69
70 pub fn map_db<F: std::error::Error>(self, f: impl FnOnce(E) -> F) -> HistoryError<F> {
75 match self {
76 Self::Db(e) => HistoryError::Db(f(e)),
77 Self::IntList(e) => HistoryError::IntList(e),
78 Self::NonContiguousBlock { expected, got } => {
79 HistoryError::NonContiguousBlock { expected, got }
80 }
81 Self::ParentHashMismatch { expected, got } => {
82 HistoryError::ParentHashMismatch { expected, got }
83 }
84 Self::DbNotEmpty => HistoryError::DbNotEmpty,
85 Self::EmptyRange => HistoryError::EmptyRange,
86 Self::NoBlocks => HistoryError::NoBlocks,
87 Self::HeightOutOfRange { height, first, last } => {
88 HistoryError::HeightOutOfRange { height, first, last }
89 }
90 }
91 }
92}