Skip to main content

musdk_common/http_client/
error.rs

1// parts of this file are derived from `reqwest` https://github.com/seanmonstar/reqwest
2//
3// Copyright (c) 2016 Sean McArthur
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21// THE SOFTWARE.
22
23use std::fmt;
24
25use borsh::{BorshDeserialize, BorshSerialize};
26
27use super::Status;
28
29/// The Errors that may occur when processing an `Request`
30#[derive(Debug, BorshSerialize, BorshDeserialize)]
31pub enum Error {
32    Builder(String),
33    Request(String),
34    Redirect(String),
35    Status(Status),
36    Body(String),
37    Decode(String),
38    Upgrade(String),
39}
40
41impl fmt::Display for Error {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        match self {
44            Error::Builder(e) => f.write_fmt(format_args!("builder error: {e:?}"))?,
45            Error::Request(e) => f.write_fmt(format_args!("error sending request: {e:?}"))?,
46            Error::Body(e) => f.write_fmt(format_args!("request or response body error: {e:?}"))?,
47            Error::Decode(e) => f.write_fmt(format_args!("error decoding response body: {e:?}"))?,
48            Error::Redirect(e) => f.write_fmt(format_args!("error following redirect {e:?}"))?,
49            Error::Upgrade(e) => f.write_fmt(format_args!("error upgrading connection {e:?}"))?,
50            Error::Status(ref status) => {
51                let prefix = if status.is_client_error() {
52                    "HTTP status client error"
53                } else {
54                    "HTTP status server error"
55                };
56                write!(f, "{prefix} ({status})")?;
57            }
58        };
59
60        Ok(())
61    }
62}