kona_derive/errors/
sources.rs

1//! Error types for sources.
2
3use crate::{PipelineError, PipelineErrorKind};
4use alloc::string::{String, ToString};
5use thiserror::Error;
6
7/// Blob Decoding Error
8#[derive(Error, Debug, PartialEq, Eq)]
9pub enum BlobDecodingError {
10    /// Invalid field element
11    #[error("Invalid field element")]
12    InvalidFieldElement,
13    /// Invalid encoding version
14    #[error("Invalid encoding version")]
15    InvalidEncodingVersion,
16    /// Invalid length
17    #[error("Invalid length")]
18    InvalidLength,
19    /// Missing Data
20    #[error("Missing data")]
21    MissingData,
22}
23
24/// An error returned by the [`BlobProviderError`].
25#[derive(Error, Debug, PartialEq, Eq)]
26pub enum BlobProviderError {
27    /// The number of specified blob hashes did not match the number of returned sidecars.
28    #[error("Blob sidecar length mismatch: expected {0}, got {1}")]
29    SidecarLengthMismatch(usize, usize),
30    /// Slot derivation error.
31    #[error("Failed to derive slot")]
32    SlotDerivation,
33    /// Blob decoding error.
34    #[error("Blob decoding error: {0}")]
35    BlobDecoding(#[from] BlobDecodingError),
36    /// Error pertaining to the backend transport.
37    #[error("{0}")]
38    Backend(String),
39}
40
41impl From<BlobProviderError> for PipelineErrorKind {
42    fn from(val: BlobProviderError) -> Self {
43        match val {
44            BlobProviderError::SidecarLengthMismatch(_, _) => {
45                PipelineError::Provider(val.to_string()).crit()
46            }
47            BlobProviderError::SlotDerivation => PipelineError::Provider(val.to_string()).crit(),
48            BlobProviderError::BlobDecoding(_) => PipelineError::Provider(val.to_string()).crit(),
49            BlobProviderError::Backend(_) => PipelineError::Provider(val.to_string()).temp(),
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57    use core::error::Error;
58
59    #[test]
60    fn test_blob_decoding_error_source() {
61        let err: BlobProviderError = BlobDecodingError::InvalidFieldElement.into();
62        assert!(err.source().is_some());
63    }
64
65    #[test]
66    fn test_from_blob_provider_error() {
67        let err: PipelineErrorKind = BlobProviderError::SlotDerivation.into();
68        assert!(matches!(err, PipelineErrorKind::Critical(_)));
69
70        let err: PipelineErrorKind = BlobProviderError::SidecarLengthMismatch(1, 2).into();
71        assert!(matches!(err, PipelineErrorKind::Critical(_)));
72
73        let err: PipelineErrorKind =
74            BlobProviderError::BlobDecoding(BlobDecodingError::InvalidFieldElement).into();
75        assert!(matches!(err, PipelineErrorKind::Critical(_)));
76    }
77}