Skip to main content

rustolio_utils/http/
error.rs

1//
2// SPDX-License-Identifier: MPL-2.0
3//
4// Copyright (c) 2026 Tobias Binnewies. All rights reserved.
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9//
10
11pub type Result<T> = std::result::Result<T, Error>;
12
13#[derive(Debug)]
14pub enum Error {
15    InvalidUri,
16    InvalidHeaders,
17    InvalidType,
18
19    Fetch,
20    Parse,
21    Recv,
22
23    #[cfg(not(target_arch = "wasm32"))]
24    Build(http::Error),
25    #[cfg(not(target_arch = "wasm32"))]
26    Body(Box<dyn std::error::Error + Send + Sync + 'static>),
27}
28
29#[cfg(not(target_arch = "wasm32"))]
30impl From<http::Error> for Error {
31    fn from(value: http::Error) -> Self {
32        Error::Build(value)
33    }
34}
35
36#[cfg(not(target_arch = "wasm32"))]
37impl Error {
38    pub fn body(e: impl std::error::Error + Send + Sync + 'static) -> Self {
39        Error::Body(Box::new(e))
40    }
41}
42
43impl std::fmt::Display for Error {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        write!(f, "{self:?}")
46    }
47}
48
49impl std::error::Error for Error {}
50
51// impl Error {
52//     fn invalid_url(e: JsValue) -> Self {
53//         Self::InvalidUrl(JSON::stringify(&e).unwrap().try_into().unwrap())
54//     }
55//     fn fetch(e: JsValue) -> Self {
56//         Self::Fetch(JSON::stringify(&e).unwrap().try_into().unwrap())
57//     }
58//     fn recv(e: JsValue) -> Self {
59//         Self::Recv(JSON::stringify(&e).unwrap().try_into().unwrap())
60//     }
61//     fn parse_json(e: serde_wasm_bindgen::Error) -> Self {
62//         Self::Parse(e.to_string())
63//     }
64//     fn parse_text(e: JsValue) -> Self {
65//         Self::Parse(JSON::stringify(&e).unwrap().try_into().unwrap())
66//     }
67//     pub fn msg(&self) -> &str {
68//         match self {
69//             Self::InvalidUrl(s) | Self::Fetch(s) | Self::Parse(s) | Self::Recv(s) => s,
70//         }
71//     }
72// }