Skip to main content

trillium_client/conn/
unexpected_status_error.rs

1use super::Conn;
2use std::{
3    error::Error,
4    fmt::{self, Debug, Display, Formatter},
5    ops::{Deref, DerefMut},
6};
7/// An unexpected http status code was received. Transform this back
8/// into the conn with [`From::from`]/[`Into::into`].
9///
10/// Currently only returned by [`Conn::success`]
11#[derive(Debug)]
12pub struct UnexpectedStatusError(Box<Conn>);
13impl From<Conn> for UnexpectedStatusError {
14    fn from(value: Conn) -> Self {
15        Self(Box::new(value))
16    }
17}
18
19impl From<UnexpectedStatusError> for Conn {
20    fn from(value: UnexpectedStatusError) -> Self {
21        *value.0
22    }
23}
24
25impl Deref for UnexpectedStatusError {
26    type Target = Conn;
27
28    fn deref(&self) -> &Self::Target {
29        &self.0
30    }
31}
32impl DerefMut for UnexpectedStatusError {
33    fn deref_mut(&mut self) -> &mut Self::Target {
34        &mut self.0
35    }
36}
37
38impl Error for UnexpectedStatusError {}
39impl Display for UnexpectedStatusError {
40    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
41        match self.status() {
42            Some(status) => f.write_fmt(format_args!(
43                "expected a success (2xx) status code, but got {status}"
44            )),
45            None => f.write_str("expected a status code to be set, but none was"),
46        }
47    }
48}