kona_derive/errors/
sources.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Error types for sources.

use super::{PipelineError, PipelineErrorKind};
use alloc::string::{String, ToString};

/// Blob Decoding Error
#[derive(derive_more::Display, Debug, PartialEq, Eq)]
pub enum BlobDecodingError {
    /// Invalid field element
    #[display("Invalid field element")]
    InvalidFieldElement,
    /// Invalid encoding version
    #[display("Invalid encoding version")]
    InvalidEncodingVersion,
    /// Invalid length
    #[display("Invalid length")]
    InvalidLength,
    /// Missing Data
    #[display("Missing data")]
    MissingData,
}

impl core::error::Error for BlobDecodingError {}

/// An error returned by the [BlobProviderError].
#[derive(derive_more::Display, Debug, PartialEq, Eq)]
pub enum BlobProviderError {
    /// The number of specified blob hashes did not match the number of returned sidecars.
    #[display("Blob sidecar length mismatch: expected {_0}, got {_1}")]
    SidecarLengthMismatch(usize, usize),
    /// Slot derivation error.
    #[display("Failed to derive slot")]
    SlotDerivation,
    /// Blob decoding error.
    #[display("Blob decoding error: {_0}")]
    BlobDecoding(BlobDecodingError),
    /// Error pertaining to the backend transport.
    #[display("{_0}")]
    Backend(String),
}

impl From<BlobProviderError> for PipelineErrorKind {
    fn from(val: BlobProviderError) -> Self {
        match val {
            BlobProviderError::SidecarLengthMismatch(_, _) => {
                PipelineError::Provider(val.to_string()).crit()
            }
            BlobProviderError::SlotDerivation => PipelineError::Provider(val.to_string()).crit(),
            BlobProviderError::BlobDecoding(_) => PipelineError::Provider(val.to_string()).crit(),
            BlobProviderError::Backend(_) => PipelineError::Provider(val.to_string()).temp(),
        }
    }
}

impl From<BlobDecodingError> for BlobProviderError {
    fn from(err: BlobDecodingError) -> Self {
        Self::BlobDecoding(err)
    }
}

impl core::error::Error for BlobProviderError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::BlobDecoding(err) => Some(err),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::error::Error;

    #[test]
    fn test_blob_decoding_error_source() {
        let err: BlobProviderError = BlobDecodingError::InvalidFieldElement.into();
        assert!(err.source().is_some());
    }

    #[test]
    fn test_from_blob_provider_error() {
        let err: PipelineErrorKind = BlobProviderError::SlotDerivation.into();
        assert!(matches!(err, PipelineErrorKind::Critical(_)));

        let err: PipelineErrorKind = BlobProviderError::SidecarLengthMismatch(1, 2).into();
        assert!(matches!(err, PipelineErrorKind::Critical(_)));

        let err: PipelineErrorKind =
            BlobProviderError::BlobDecoding(BlobDecodingError::InvalidFieldElement).into();
        assert!(matches!(err, PipelineErrorKind::Critical(_)));
    }
}