retina_fetch/
error.rs

1// Copyright (C) 2023 Tristan Gerritsen <tristan@thewoosh.org>
2// All Rights Reserved.
3
4use std::fmt::Display;
5
6use crate::FetchResponse;
7
8/// An error that occurred within the retina-fetch crate.
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub enum InternalError {
11    /// An unknown error occurred within [hyper].
12    HyperError,
13
14    /// The internal synchronization mechanisms failed.
15    SynchronizationFault,
16}
17
18impl Display for InternalError {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        core::fmt::Debug::fmt(&self, f)
21    }
22}
23
24/// An error that can occur within the library or network stack.
25#[derive(Clone, Debug, PartialEq, Eq)]
26pub enum Error {
27    /// A network-related error.
28    NetworkError(NetworkError),
29
30    /// An error internal to the retina-fetch crate.
31    InternalError(InternalError),
32}
33
34impl Display for Error {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        core::fmt::Debug::fmt(&self, f)
37    }
38}
39
40impl std::error::Error for Error {
41}
42
43/// A network error occurred whilst processing the request/response.
44#[derive(Clone, Debug, PartialEq, Eq)]
45pub enum NetworkError {
46    /// Some unknown error occurred.
47    Generic,
48
49    /// A `file://` URL was not found.
50    LocalFileNotFound,
51}
52
53impl Display for NetworkError {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        core::fmt::Debug::fmt(&self, f)
56    }
57}
58
59impl From<InternalError> for FetchResponse {
60    fn from(value: InternalError) -> Self {
61        Err(value.into())
62    }
63}
64
65impl From<hyper::Error> for Error {
66    fn from(value: hyper::Error) -> Self {
67        // TODO
68        _ = value;
69        Error::NetworkError(NetworkError::Generic)
70    }
71}
72
73impl From<InternalError> for Error {
74    fn from(value: InternalError) -> Self {
75        Self::InternalError(value)
76    }
77}
78
79impl From<Error> for FetchResponse {
80    fn from(value: Error) -> Self {
81        Err(value)
82    }
83}