iroh_blobs/get/
error.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! Error returned from get operations
use std::io;

use iroh::endpoint::{ConnectionError, ReadError, VarInt, WriteError};
use n0_snafu::SpanTrace;
use nested_enum_utils::common_fields;
use snafu::{Backtrace, Snafu};

use crate::get::fsm::{
    AtBlobHeaderNextError, AtClosingNextError, ConnectedNextError, DecodeError, InitialNextError,
};

/// Failures for a get operation
#[common_fields({
    backtrace: Option<Backtrace>,
    #[snafu(implicit)]
    span_trace: SpanTrace,
})]
#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))]
#[snafu(module)]
pub enum GetError {
    #[snafu(transparent)]
    InitialNext {
        source: InitialNextError,
    },
    #[snafu(transparent)]
    ConnectedNext {
        source: ConnectedNextError,
    },
    #[snafu(transparent)]
    AtBlobHeaderNext {
        source: AtBlobHeaderNextError,
    },
    #[snafu(transparent)]
    Decode {
        source: DecodeError,
    },
    #[snafu(transparent)]
    IrpcSend {
        source: irpc::channel::SendError,
    },
    #[snafu(transparent)]
    AtClosingNext {
        source: AtClosingNextError,
    },
    LocalFailure {
        source: anyhow::Error,
    },
    BadRequest {
        source: anyhow::Error,
    },
}

impl GetError {
    pub fn iroh_error_code(&self) -> Option<VarInt> {
        if let Some(ReadError::Reset(code)) = self
            .remote_read()
            .and_then(|source| source.get_ref())
            .and_then(|e| e.downcast_ref::<iroh::endpoint::ReadError>())
        {
            Some(*code)
        } else if let Some(WriteError::Stopped(code)) = self
            .remote_write()
            .and_then(|source| source.get_ref())
            .and_then(|e| e.downcast_ref::<iroh::endpoint::WriteError>())
        {
            Some(*code)
        } else if let Some(ConnectionError::ApplicationClosed(ac)) = self
            .open()
            .and_then(|source| source.get_ref())
            .and_then(|e| e.downcast_ref::<iroh::endpoint::ConnectionError>())
        {
            Some(ac.error_code)
        } else {
            None
        }
    }

    pub fn remote_write(&self) -> Option<&io::Error> {
        match self {
            Self::ConnectedNext {
                source: ConnectedNextError::Write { source, .. },
                ..
            } => Some(source),
            _ => None,
        }
    }

    pub fn open(&self) -> Option<&io::Error> {
        match self {
            Self::InitialNext {
                source: InitialNextError::Open { source, .. },
                ..
            } => Some(source),
            _ => None,
        }
    }

    pub fn remote_read(&self) -> Option<&io::Error> {
        match self {
            Self::AtBlobHeaderNext {
                source: AtBlobHeaderNextError::Read { source, .. },
                ..
            } => Some(source),
            Self::Decode {
                source: DecodeError::Read { source, .. },
                ..
            } => Some(source),
            Self::AtClosingNext {
                source: AtClosingNextError::Read { source, .. },
                ..
            } => Some(source),
            _ => None,
        }
    }

    pub fn local_write(&self) -> Option<&io::Error> {
        match self {
            Self::Decode {
                source: DecodeError::Write { source, .. },
                ..
            } => Some(source),
            _ => None,
        }
    }
}

pub type GetResult<T> = std::result::Result<T, GetError>;