reqwest_streams/
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
//! Error types for streaming responses.

use std::fmt;

type BoxedError = Box<dyn std::error::Error + Send + Sync>;

/// The error that may occur when attempting to stream a [`reqwest::Response`].
pub struct StreamBodyError {
    kind: StreamBodyKind,
    source: Option<BoxedError>,
    message: Option<String>,
}

impl StreamBodyError {
    /// Create a new instance of an error.
    pub fn new(kind: StreamBodyKind, source: Option<BoxedError>, message: Option<String>) -> Self {
        Self {
            kind,
            source,
            message,
        }
    }

    /// The kind of error that occurred during streaming.
    pub fn kind(&self) -> StreamBodyKind {
        self.kind
    }

    /// The actual error that occurred.
    pub fn source(&self) -> Option<&BoxedError> {
        self.source.as_ref()
    }

    /// The message associated with the error.
    pub fn message(&self) -> Option<&str> {
        self.message.as_deref()
    }
}

/// The kind of error that occurred during streaming.
#[derive(Clone, Copy, Debug)]
pub enum StreamBodyKind {
    /// An error occured while decoding a frame or format.
    CodecError,

    /// An error occured while reading the stream.
    InputOutputError,

    /// The maximum object length was exceeded.
    MaxLenReachedError,
}

impl fmt::Debug for StreamBodyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut builder = f.debug_struct("reqwest::Error");

        builder.field("kind", &self.kind);

        if let Some(ref source) = self.source {
            builder.field("source", source);
        }

        if let Some(ref message) = self.message {
            builder.field("message", message);
        }

        builder.finish()
    }
}

impl fmt::Display for StreamBodyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.kind {
            StreamBodyKind::CodecError => f.write_str("Frame/codec error")?,
            StreamBodyKind::InputOutputError => f.write_str("I/O error")?,
            StreamBodyKind::MaxLenReachedError => f.write_str("Max object length reached")?,
        };

        if let Some(message) = &self.message {
            write!(f, ": {}", message)?;
        }

        if let Some(e) = &self.source {
            write!(f, ": {}", e)?;
        }

        Ok(())
    }
}

impl std::error::Error for StreamBodyError {}

impl From<std::io::Error> for StreamBodyError {
    fn from(err: std::io::Error) -> Self {
        StreamBodyError::new(StreamBodyKind::InputOutputError, Some(Box::new(err)), None)
    }
}