Skip to main content

simple_hyper_client/
error.rs

1/* Copyright (c) Fortanix, Inc.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7use derive_more::{IsVariant, TryUnwrap, Unwrap};
8use hyper::Method;
9use std::error;
10
11pub type HyperClientError = hyper_util::client::legacy::Error;
12
13#[derive(Debug, thiserror::Error, IsVariant, TryUnwrap, Unwrap)]
14#[try_unwrap(ref)]
15#[unwrap(ref)]
16pub enum Error {
17    #[error(transparent)]
18    Http(#[from] http::Error),
19
20    #[error("{0}{source}", source = render_source_error(.0))]
21    Client(#[from] HyperClientError),
22
23    #[error("{0} requests are not allowed to have a body")]
24    BodyNotAllowed(Method),
25}
26
27fn render_source_error(err: &HyperClientError) -> String {
28    if let Some(err) = error::Error::source(err) {
29        format!(": {err}")
30    } else {
31        Default::default()
32    }
33}