reqwest_streams/
error.rs

1//! Error types for streaming responses.
2
3use std::fmt;
4
5type BoxedError = Box<dyn std::error::Error + Send + Sync>;
6
7/// The error that may occur when attempting to stream a [`reqwest::Response`].
8pub struct StreamBodyError {
9    kind: StreamBodyKind,
10    source: Option<BoxedError>,
11    message: Option<String>,
12}
13
14impl StreamBodyError {
15    /// Create a new instance of an error.
16    pub fn new(kind: StreamBodyKind, source: Option<BoxedError>, message: Option<String>) -> Self {
17        Self {
18            kind,
19            source,
20            message,
21        }
22    }
23
24    /// The kind of error that occurred during streaming.
25    pub fn kind(&self) -> StreamBodyKind {
26        self.kind
27    }
28
29    /// The actual error that occurred.
30    pub fn source(&self) -> Option<&BoxedError> {
31        self.source.as_ref()
32    }
33
34    /// The message associated with the error.
35    pub fn message(&self) -> Option<&str> {
36        self.message.as_deref()
37    }
38}
39
40/// The kind of error that occurred during streaming.
41#[derive(Clone, Copy, Debug)]
42pub enum StreamBodyKind {
43    /// An error occured while decoding a frame or format.
44    CodecError,
45
46    /// An error occured while reading the stream.
47    InputOutputError,
48
49    /// The maximum object length was exceeded.
50    MaxLenReachedError,
51}
52
53impl fmt::Debug for StreamBodyError {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        let mut builder = f.debug_struct("reqwest::Error");
56
57        builder.field("kind", &self.kind);
58
59        if let Some(ref source) = self.source {
60            builder.field("source", source);
61        }
62
63        if let Some(ref message) = self.message {
64            builder.field("message", message);
65        }
66
67        builder.finish()
68    }
69}
70
71impl fmt::Display for StreamBodyError {
72    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73        match self.kind {
74            StreamBodyKind::CodecError => f.write_str("Frame/codec error")?,
75            StreamBodyKind::InputOutputError => f.write_str("I/O error")?,
76            StreamBodyKind::MaxLenReachedError => f.write_str("Max object length reached")?,
77        };
78
79        if let Some(message) = &self.message {
80            write!(f, ": {}", message)?;
81        }
82
83        if let Some(e) = &self.source {
84            write!(f, ": {}", e)?;
85        }
86
87        Ok(())
88    }
89}
90
91impl std::error::Error for StreamBodyError {}
92
93impl From<std::io::Error> for StreamBodyError {
94    fn from(err: std::io::Error) -> Self {
95        StreamBodyError::new(StreamBodyKind::InputOutputError, Some(Box::new(err)), None)
96    }
97}